MultipartEntityBuilder and Charset(MultipartEntityBuilder 和字符集)
问题描述
我升级了我的 httpmime 包,现在我的字符串没有以 UTF-8 格式发送或接收
I upgraded my httpmime package, and now my strings are not sent or received as UTF-8
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
Charset chars = Charset.forName("UTF-8");
entity.setCharset(chars);
entity.addTextBody("some_text", some_text);
HttpPost httppost = new HttpPost(url);
httppost.setEntity(entity.build());
...and so on..
我错过了什么?我曾经构建一个 StringBody 并在 stringbody 中设置字符集,但现在已弃用,它似乎不起作用
what am I missing? I used to build a StringBody and set the charset in the stringbody, but that is deprecated now, and it just doesn't seem to work
推荐答案
解决了 :) 原来 ContentType 现在很重要,我发送的是纯文本,还有一些 JSON 文本,
Solved it :) it turns out that ContentType is now important, and I was sending text that was plain, and also some text that was JSON,
对于纯文本,您可以使用:
for the plain text, you can use:
entity.addTextBody("plain_text",plain_text,ContentType.TEXT_PLAIN);
对于 JSON:
entity.addTextBody("json_text",json_text,ContentType.APPLICATION_JSON);
这样字符集也适用于 JSON 字符串(很奇怪,但现在可以了)
that way the charset also works on JSON strings (weird, but now OK)
这篇关于MultipartEntityBuilder 和字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MultipartEntityBuilder 和字符集


基础教程推荐
- Java:带有char数组的println给出乱码 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01