为什么Retrofit无法使用方括号正确编码查询字符串? [英] Why Retrofit cannot encode properly query string with square brackets?

查看:135
本文介绍了为什么Retrofit无法使用方括号正确编码查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Android应用中使用Retrofit作为我的网络层,但我对URL编码有疑问。

I'm using Retrofit for my network layer in my Android app, but I have a problem with the URL encoding.

我必须调用REST API之类的那:

I have to call an REST API like that:

https://my_hostname.com/some_path?q=some_query&param[0]=value1&param[1]=value2&other_param=abcd

因为你可以看到查询字符串是由某种不同类型的参数,所以我决定在Retrofit接口中使用 @QueryMap 注释,并使用 Map< String,String> 其中
q,param [1],param [0],other_param 是地图的字符串键

as you can see the query string is composed by some different kind of parameters, so I decided to use the @QueryMap annotation in the Retrofit Interface with a Map<String, String> where q, param[1], param[0], other_param are String keys of the map

我期待什么?
我希望URL中的方括号用%5B 编码为'['%5D 对于'[',但这不会发生。

What do I expect? I expect the square brackets in the URL are encoded with %5B for '[' and %5D for '[', but this does not happen.

为什么会这样?方括号应使用百分比编码进行编码。这是一个错误还是我做错了什么?我也尝试了 @EncodedQueryMap 注释,没有任何区别。

Why does this happen? The square brackets should be encoded with percent encoding. Is this a bug or I'm doing something wrong? I also tried the @EncodedQueryMap annotation with no differences.

推荐答案

查询名称永远不会进行URL编码。

Query names are never URL encoded.

@QueryMap 状态:


值是URL编码的。

Values are URL encoded.

对于 @EncodedQueryMap


值不是URL编码的。

Values are not URL encoded.

但是,我只是提交了拉取请求以稍微改变这种行为。我通过使用 @Query(value =..,encodeName = true) @QueryMap(encodeNames = true)添加对编码键的支持

However, I just submitted a pull request to change this behavior a bit. I am adding support for encoding keys by using @Query(value = "..", encodeName = true) or @QueryMap(encodeNames = true).

这篇关于为什么Retrofit无法使用方括号正确编码查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆