我有一种方法,可以使用MultipartEntity内容类型发送图像和文本作为HttpPost。对于英文符号,一切都很好,但是对于Unicode符号(例如Cyrliics),它只发送???。因此,我想知道如何正确设置MultipartEntity的UTF-8编码,因为我已经尝试了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);
更新 我尝试按照@Donaudampfschifffreizeitfahrt的建议使用InputStream。现在我得到了���字符。
InputStream stream = new ByteArrayInputStream(mMessage.getBytes(Charset.forName("UTF-8")));
mpEntity.addBinaryBody("message", stream);
我也尝试过以下方法:
mpEntity.addBinaryBody("message", mMessage.getBytes(Charset.forName("UTF-8")));