1

Code for posting photos and some data to server.

HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(Constants.STORIES_URL);
                httpPost.addHeader("Authorization", "Token token=" + s);

                MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("uuid", "1234567890"));
                nameValuePairs.add(new BasicNameValuePair("title", "Title"));
                nameValuePairs.add(new BasicNameValuePair("subtitle", "Subtitle"));
                nameValuePairs.add(new BasicNameValuePair("private", "true"));
                nameValuePairs.add(new BasicNameValuePair("photo", Environment.getExternalStorageDirectory() + "/DCIM/0.jpg"));
                nameValuePairs.add(new BasicNameValuePair("bytes[uuid]", "1234567890"));
                nameValuePairs.add(new BasicNameValuePair("bytes[timelineDate]", "1970-01-01T00:00:00.000+00:00"));
                nameValuePairs.add(new BasicNameValuePair("bytes[caption]", "Byte 1"));
                nameValuePairs.add(new BasicNameValuePair("bytes[photo]", Environment.getExternalStorageDirectory() + "/DCIM/0.jpg"));

                for (NameValuePair nameValuePair : nameValuePairs) {
                    if (nameValuePair.getName().equalsIgnoreCase("photo") || nameValuePair.getName().equalsIgnoreCase("bytes[photo]")) {
                        File imgFile = new File(nameValuePair.getValue());
                        FileBody fileBody = new FileBody(imgFile, "image/jpeg");
                        multipartEntity.addPart("story[photo]", fileBody);
                    } else {
                        multipartEntity.addPart("story[" + nameValuePair.getName() + "]", new StringBody(nameValuePair.getValue()));
                    }
                }              

                httpPost.setEntity(multipartEntity);
                response = httpClient.execute(httpPost);

And in response I got error Completed 500 Internal Server Error: "argumenterror invalid byte sequence in utf-8"

How to resolve this error?

1 Answer 1

0

The answer is:

HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(Constants.STORIES_URL);
                httpPost.addHeader("Authorization", "Token token=" + s);

                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("uuid", "1234567890"));
                nameValuePairs.add(new BasicNameValuePair("title", "My Title"));
                nameValuePairs.add(new BasicNameValuePair("subtitle", "My Subtitle"));
                nameValuePairs.add(new BasicNameValuePair("private", "true"));
                nameValuePairs.add(new BasicNameValuePair("photo", Environment.getExternalStorageDirectory() + "/DCIM/0.jpg"));
                nameValuePairs.add(new BasicNameValuePair("bytes[uuid]", "1234567890"));
                nameValuePairs.add(new BasicNameValuePair("bytes[timelineDate]", "1970-01-01T00:00:00.000+00:00"));
                nameValuePairs.add(new BasicNameValuePair("bytes[caption]", "Byte 1"));
                nameValuePairs.add(new BasicNameValuePair("bytes[photo]", Environment.getExternalStorageDirectory() + "/DCIM/0.jpg"));

                for (NameValuePair nameValuePair : nameValuePairs) {
                    if (nameValuePair.getName().equalsIgnoreCase("photo") || nameValuePair.getName().equalsIgnoreCase("bytes[photo]")) {
                        File imgFile = new File(nameValuePair.getValue());
                        builder.addBinaryBody("story[photo]", imgFile, ContentType.create("image/jpg"), "photo.jpg");
                    } else {
                        builder.addTextBody("story[" + nameValuePair.getName() + "]", nameValuePair.getValue());
                    }
                }

                HttpEntity build = builder.build();
                httpPost.setEntity(build);
                response = httpClient.execute(httpPost);
                String responseString = new BasicResponseHandler().handleResponse(response);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.