问题描述
我已经配置了Apache HttpClient的,像这样:
I have configured the apache httpClient like so:
HttpProtocolParams.setContentCharset(httpParameters, "UTF-8");
HttpProtocolParams.setHttpElementCharset(httpParameters, "UTF-8");
我还包括HTTP头内容类型:应用程序/ JSON;字符集= UTF-8的所有HTTP POST和PUT请求
I also include the http header "Content-Type: application/json; charset=UTF-8" for all http post and put requests.
我想发送HTTP使用包含特殊字符的JSON主体立柱/ PUT请求(即通过谷歌拼音键盘,符号等中国字)字符显示为乱码的日志中,但我认为这是因为DDMS不支持UTF-8,如descibed在这个的问题。
I am trying to send http post/put requests with a json body that contains special characters (ie. chinese characters via the Google Pinyin keyboard, symbols, etc.) The characters appear as gibberish in the logs but I think this is because DDMS does not support UTF-8, as descibed in this issue.
现在的问题是,当服务器收到请求时,它有时不能看到所有的字符(尤其是中国文字),或者变得毫无意义的垃圾,当我们通过一个GET请求检索它。
The problem is when the server receives the request, it sometimes doesn't see the characters at all (especially the Chinese characters), or it becomes meaningless garbage when we retrieve it through a GET request.
我也试过把250非ASCII字符在一个领域,因为特定领域应该可以长达250个字符。但是,它无法验证在服务器端它声称,250个字符的限制已经被超过。 250 ASCII字符的工作就好了。
I also tried putting 250 non-ascii characters in a single field because that particular field should be able to take up to 250 characters. However, it fails to validate at the server side which claims that the 250 character limit has been exceeded. 250 ASCII characters work just fine.
服务器帅哥声称他们支持UTF-8。他们甚至模仿包含中国字符的POST请求,并接收服务器就好了数据。但是,这个家伙(一个中国人)正在使用Windows计算机安装了中国语言包(我想,因为他可以输入中国字他的键盘上)。
The server dudes claim that they support UTF-8. They even tried simulating a post request that contains Chinese characters, and the data was received by the server just fine. However, the guy (a Chinese guy) is using a Windows computer with the Chinese language pack installed (I think, because he can type Chinese characters on his keyboard).
我猜测,正在使用的Android客户端和服务器(由中国人BTW制造)的字符集不对齐。但我不知道哪一个是有过错的,因为服务器帅哥声称他们支持UTF-8,而我们其余的客户端配置为支持UTF-8。
I'm guessing that the charsets being used by the Android client and the server (made by Chinese guys btw) are not aligned. But I do not know which one is at fault since the server dudes claim that they support UTF-8, and our rest client is configured to support UTF-8.
这让我想知道在Android上使用什么样的字符集默认情况下,所有的文本输入,如果它可以改变一个不同的编程方式。我试图找到如何做到这一点的输入控件资源,但我没有找到什么有用的东西。
This got me wondering on what charset Android uses by default on all text input, and if it can be changed to a different one programatically. I tried to find resources on how to do this on input widgets but I did not find anything useful.
有没有一种方法来设置字符集为Android的所有输入窗口小部件?或者,也许我错过了一些在其余客户端配置?或者,也许,只是也许,服务器帅哥不使用UTF-8,在他们的服务器和使用的Windows字符集字符代替?
Is there a way to set the charset for all input widgets in Android? Or maybe I missed something in the rest client configuration? Or maybe, just maybe, the server dudes are not using UTF-8 at their servers and used Windows charsets instead?
推荐答案
显然,我忘了给StringEntity的字符集设置为UTF-8。这些线做的伎俩:
Apparently, I forgot to set the StringEntity's charset to UTF-8. These lines did the trick:
httpPut.setEntity(new StringEntity(body, HTTP.UTF_8));
httpPost.setEntity(new StringEntity(body, HTTP.UTF_8));
所以,至少有两层,非ASCII字符发送一个HTTP POST时设置在Android客户端的字符集。
So, there are at least two levels to set the charset in the Android client when sending an http post with non-ascii characters.
- 在其余客户端本身本身
- 的StringEntity
更新:正如塞缪尔指出,在评论中,现代的方式来做到这一点是使用的ContentType,像这样:
UPDATE: As Samuel pointed out in the comments, the modern way to do it is to use a ContentType, like so:
final StringEntity se = new StringEntity(body, ContentType.APPLICATION_JSON);
httpPut.setEntity(se);
这篇关于Android的默认字符集发送HTTP POST /把的时候 - 有特殊字符的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!