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?