发送 http post/put 时的 Android 默认字符集 - 特殊字符问题
我已经像这样配置了 apache httpClient:
HttpProtocolParams.setContentCharset(httpParameters, "UTF-8");
HttpProtocolParams.setHttpElementCharset(httpParameters, "UTF-8");
我还为所有 http post 和 put 请求包含 http 标头“Content-Type: application/json; charset=UTF-8”。
我正在尝试发送带有包含特殊字符(即通过谷歌拼音键盘的中文字符、符号等)的 json 正文的 http post/put 请求,这些字符在日志中显示为乱码,但我认为这是因为 DDMS 确实不支持 UTF-8,如此问题中所述。
问题是,当服务器收到请求时,有时根本看不到这些字符(尤其是汉字),或者当我们通过 GET 请求检索它时,它就变成了毫无意义的垃圾。
我还尝试将 250 个非 ascii 字符放入单个字段中,因为该特定字段应该最多可以容纳 250 个字符。但是,它无法在服务器端进行验证,并声称已超出 250 个字符的限制。 250 个 ASCII 字符就可以了。
服务器家伙声称他们支持 UTF-8。他们甚至尝试模拟一个包含汉字的post请求,服务器也很好地接收到了数据。然而,这个家伙(一个中国人)正在使用安装了中文语言包的Windows计算机(我想,因为他可以在键盘上输入汉字)。
我猜测 Android 客户端和服务器(由中国人制造)使用的字符集不一致。但我不知道哪一个有问题,因为服务器声称他们支持 UTF-8,而我们的其余客户端配置为支持 UTF-8。
这让我想知道 Android 在所有文本输入上默认使用什么字符集,以及是否可以通过编程将其更改为不同的字符集。我试图找到有关如何在输入小部件上执行此操作的资源,但没有找到任何有用的东西。
有没有办法为 Android 中的所有输入小部件设置字符集?或者也许我错过了其余客户端配置中的某些内容?或者,也许,只是也许,服务器人员没有在他们的服务器上使用 UTF-8,而是使用 Windows 字符集?
I have configured the apache httpClient like so:
HttpProtocolParams.setContentCharset(httpParameters, "UTF-8");
HttpProtocolParams.setHttpElementCharset(httpParameters, "UTF-8");
I also include the http header "Content-Type: application/json; charset=UTF-8" for all http post and put requests.
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.
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.
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.
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).
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.
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
显然,我忘记将 StringEntity 的字符集设置为 UTF-8。这些行成功了:
因此,在发送带有非 ASCII 字符的 http post 时,至少有两个级别可以在 Android 客户端中设置字符集。
更新:正如 Samuel 在评论中指出的那样,现代的方法是使用 ContentType,如下所示:
Apparently, I forgot to set the StringEntity's charset to UTF-8. These lines did the trick:
So, there are at least two levels to set the charset in the Android client when sending an http post with non-ascii characters.
UPDATE: As Samuel pointed out in the comments, the modern way to do it is to use a ContentType, like so:
我知道这篇文章有点旧,但这里有一个解决方案:
这是我的代码,用于将 UTF-8 字符串(无论它们是 xml Soap 还是 json)发布到服务器。我尝试使用西里尔字母、哈希值和其他一些特殊字符,效果非常好。它是我通过论坛找到的许多解决方案的汇编。
我希望有人会发现这段代码很有帮助。 :)
I know this post is a bit old but nevertheless here is a solution:
Here is my code for posting UTF-8 strings (it doesn't matter if they are xml soap or json) to a server. I tried it with cyrillic, hash values and some other special characters and it works like a charm. It is a compilation of many solutions I found through the forums.
I hope that someone will find this code helpful. :)
您应该将字符串实体的字符集设置为 UTF-8:
You should set charset of your string entity to UTF-8:
您可以使用
curl
发送相同的数据来消除服务器问题。如果它与
curl
一起使用,请使用--trace
检查输出。确保您以字节形式发送内容正文。将来自 Android 的 HTTP 请求与成功
curl
请求的输出进行比较。You can eliminate the server as the problem by using
curl
to send the same data.If it works with
curl
use--trace
to check the output.Ensure you are sending the content body as bytes. Compare the HTTP request from Android with the output from the successful
curl
request.