gpt4 book ai didi

android - 通过 MultipartEntity 发送 Unicode 字符

转载 作者:可可西里 更新时间:2023-11-01 16:56:11 25 4
gpt4 key购买 nike

我有一种使用 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);

UPD我尝试按照@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")));

最佳答案

我用不同的方式解决了它,使用:

builder.addTextBody(key, שלום, ContentType.TEXT_PLAIN.withCharset("UTF-8"));

关于android - 通过 MultipartEntity 发送 Unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25665178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com