简体   繁体   English

从Android上载图片时,在Ruby服务器上,utf-8错误中的argumentserror字节序列无效

[英]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.

相关问题 Android studio 2字节UTF-8序列的无效字节2 - Android studio Invalid byte 2 of 2-byte UTF-8 sequence “ 1字节UTF-8序列的无效字节1”错误 - “Invalid byte 1 of 1-byte UTF-8 sequence” error JAXB错误的说明:1字节UTF-8序列的字节1无效 - Explanation of JAXB error: Invalid byte 1 of 1-byte UTF-8 sequence 2 字节 UTF-8 序列的无效字节 2 - invalid byte 2 of 2-byte UTF-8 sequence Spring,Hibernate:尝试保存图像时出现无效的 UTF-8 起始字节 0x80 错误 - Spring, Hibernate: Invalid UTF-8 start byte 0x80 error when trying to save images 在Windows中使用Java读取UTF-8格式的xml -file会给出“ IOException:2字节UTF-8序列的无效字节2。” -error - Reading xml -file in UTF-8 format in Windows with Java gives “IOException: Invalid byte 2 of 2-byte UTF-8 sequence.” -error 2 字节 UTF-8 Java 的无效字节 2,序列错误取决于 Windows/IntelliJ - Invalid byte 2 of 2-byte UTF-8 Java, sequence error depending on Windows/IntelliJ 在.jar中而不是在eclpise中发布xml时,出现“ 1字节UTF-8序列的无效字节1” - “Invalid byte 1 of 1-byte UTF-8 sequence” occurs when posting xml in .jar but not in eclpise MalformedByteSequenceException:1字节UTF-8序列的无效字节1。 使用希伯来字符时 - MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence. when using Hebrew chars 当我执行Build项目时,三字节UTF-8序列的无效字节2 - Invalid byte 2 of a 3-byte UTF-8 sequence when i execute the Build project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM