[英]argumenterror invalid byte sequence in utf-8 error on Ruby server when uploading images from Android
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" 并且作为响应,我得到了错误:Completed 500 Internal Server Error:“ utf-8中的argumenterror无效字节序列”
How to resolve this error? 如何解决这个错误?
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.