mol*_*mol 2 apache android http multipartform-data utf-8
我有一种使用 MultipartEntity 内容类型将图像和文本作为 HttpPost 发送的方法。一切都适用于英文符号,但对于 unicode 符号(例如 Cyrliics),它只发送 ???。所以,我想知道如何正确地为 MultipartEntity 设置 UTF-8 编码,因为我已经尝试了几个 sulutions,建议在 SO 上,但没有一个工作。这是我已经拥有的:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
MultipartEntityBuilder mpEntity = MultipartEntityBuilder.create();
mpEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
mpEntity.setCharset(Consts.UTF_8);
mpEntity.addPart("image", new FileBody(new File(attachmentUri), ContentType.APPLICATION_OCTET_STREAM));
ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
StringBody stringBody = new StringBody(mMessage, contentType);
mpEntity.addPart("message", stringBody);
final HttpEntity fileBody = mpEntity.build();
httpPost.setEntity(fileBody);
HttpResponse httpResponse = httpclient.execute(httpPost);
Run Code Online (Sandbox Code Playgroud)
UPD 我尝试按照@Donaudampfschifffreizeitfahrt 的建议使用 InputStream。现在我得到了???人物。
InputStream stream = new ByteArrayInputStream(mMessage.getBytes(Charset.forName("UTF-8")));
mpEntity.addBinaryBody("message", stream);
Run Code Online (Sandbox Code Playgroud)
还试过:
mpEntity.addBinaryBody("message", mMessage.getBytes(Charset.forName("UTF-8")));
Run Code Online (Sandbox Code Playgroud)
我以不同的方式解决了它,使用:
builder.addTextBody(key, ????, ContentType.TEXT_PLAIN.withCharset("UTF-8"));
Run Code Online (Sandbox Code Playgroud)