MultipartEntityBuilder和Charset [英] MultipartEntityBuilder and Charset
本文介绍了MultipartEntityBuilder和Charset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我升级了我的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中设置charset,但是现在已经过时了,它似乎不工作。
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字符串(奇怪,但现在OK)
that way the charset also works on JSON strings (weird, but now OK)
这篇关于MultipartEntityBuilder和Charset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文