Retrofit2 urlencode 编码的问题

前言

很抱歉,造成了将近2000位同仁看了之前的这篇博客,造成了错误的理解,在此说声抱歉,重写一次博客,修正一下之前的有问题的理解。

Retorfit Urlencode

其实Retorfit并不管理urlencode。所以所有关于Retorfit 设置Urlencode的方式都是无效的。
Github 上关于 Urlencode 相关问题中 JakeWharton 的回复

So Retrofit doesn’t actually control this. It comes from OkHttp’s FormBody type which uses this list for encoding. However, your server should be percent-decoding these values to the same thing. The whole point of percent encoding is to avoid ambiguity. In theory you could percent encode every single character. That said, the current set comes from the behavior of major browsers (Chrome and Firefox) so someone posting from an HTML form would produce the same version.

关于encoded 的设置

还是 JakeWharton在GitHub上的一个回复作为解释

encoded=true means your value was already encoded with respect to the requirements of form URL bodies. As you found, we simply tell OkHttp that this was the case, and if it meets OkHttp’s requirements for what passes as properly encoded it will be included unmolested. Otherwise OkHttp tries to correct the lack of proper encoding. It does not mean that the raw value you pass will always be used.

说的很清楚,这个值仅仅是一个建议值,它用来给Okhttp建议说,这个已经编码过了,但是如果在Okhttp检查的时候,这个不符合Urlencode的规则,那么还是会给你编码,而不是设置了这个值就使用你传递的字符串进行交互。

可以在这里额外考虑一下,如果设置为True 自行使用UrlEncode工具格式化之后,是否可以修复自动UrlEncode时出现的字符集问题?没测试过,只是一个猜想。。。

传递中文

github上有个问题
https://github.com/square/retrofit/issues/1815
其实就是传递中文字符串,开发组成员,回复的很直接,浏览器怎么做,Okhttp就怎么做了。所以汉字肯定是会在Get请求时,使用Urlencode编码的。

最终的建议

老老实实遵循Http协议。汉字还是不要用@GET @Query来传递了,用@POST @Field 或 @Body 来传递吧。
给大家带来的疑惑,非常抱歉。。

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值