Retrofit在查询参数中添加随机数字吗?

huangapple go评论78阅读模式
英文:

Retrofit adds random number in query parameter?

问题

我正在调用一个GET API,该API接受一个查询参数 "par",查询参数包含一个加密字符串。加密字符串中包含JSON数据,在调用API时会出现问题,随机字符串被添加进去。例如,这是一个加密字符串:

"0wDs7cLFw63nXIQc%2FodiEJr0DWgJJJGj%2ByALAIaN%2F0TnYKp7yDYcKjE8Kftf%2Bg7tA68DMz5g%2FhJAfmKhzOKVESmrw0OUviTiFz1yQI2IQPM%3D"

但是当我调用API时,相同的字符串变成了:

"0wDs7cLFw63nXIQc%252FodiEJr0DWgJJJGj%252ByALAIaN%252F0TnYKp7yDYcKjE8Kftf%252Bg7tA68DMz5g%252FhJAfmKhzOKVESmrw0OUviTiFz1yQI2IQPM%253D"

以下是我的代码:

@GET("sendotp")
Call<ResponseBody> getKey(@QueryMap Map<String, String> map);

Map<String, String> jsonMap = new HashMap<String, String>();

jsonMap.put("par", "0wDs7cLFw63nXIQc%2FodiEJr0DWgJJJGj%2ByALAIaN%2F0TnYKp7yDYcKjE8Kftf%2Bg7tA68DMz5g%2FhJAfmKhzOKVESmrw0OUviTiFz1yQI2IQPM%3D");

Call<ResponseBody> callSendOTP = RetrofitSingleton.getInstance().get_Api().getKey(jsonMap);

使用拦截器,我得到了以下链接:

https://http://046ba9a626ae.ngrok.io/webresources/generic/key?par=0wDs7cLFw63nXIQc%252FodiEJr0DWgJJJGj%252ByALAIaN%252F0TnYKp7yDYcKjE8Kftf%252Bg7tA68DMz5g%252FhJAfmKhzOKVESmrw0OUviTiFz1yQI2IQPM%253D

注意:这只是代码和URL的翻译,不包括问题的回答。

英文:

I am calling a GET API which takes a query parameter, "par", the query parameter contains an encrypted string. The encrypted string contains a JSON data, the issue occurs when the API is called, random strings are added. For instance, this is an encrypted string

&quot;0wDs7cLFw63nXIQc%2FodiEJr0DWgJJJGj%2ByALAIaN%2F0TnYKp7yDYcKjE8Kftf%2Bg7tA68DMz5g%2FhJAfmKhzOKVESmrw0OUviTiFz1yQI2IQPM%3D&quot;

but when I make the API call the same string becomes

&quot;0wDs7cLFw63nXIQc%252FodiEJr0DWgJJJGj%252ByALAIaN%252F0TnYKp7yDYcKjE8Kftf%252Bg7tA68DMz5g%252FhJAfmKhzOKVESmrw0OUviTiFz1yQI2IQPM%253D&quot;

Here is my code:

  @GET(&quot;sendotp&quot;)
        Call&lt;ResponseBody&gt; getKey(@QueryMap Map&lt;String,String&gt; map);

  Map&lt;String,String&gt; jsonMap=new HashMap&lt;String,String&gt;();
        

jsonMap.put(&quot;par&quot;,&quot;0wDs7cLFw63nXIQc%2FodiEJr0DWgJJJGj%2ByALAIaN%2F0TnYKp7yDYcKjE8Kftf%2Bg7tA68DMz5g%2FhJAfmKhzOKVESmrw0OUviTiFz1yQI2IQPM%3D&quot;);

Call&lt;ResponseBody&gt; callSendOTP=RetrofitSingleton.getInstance().get_Api().getKey(jsonMap);

Using interceptor, I got this:

https://http://046ba9a626ae.ngrok.io/webresources/generic/key?par=0wDs7cLFw63nXIQc%252FodiEJr0DWgJJJGj%252ByALAIaN%252F0TnYKp7yDYcKjE8Kftf%252Bg7tA68DMz5g%252FhJAfmKhzOKVESmrw0OUviTiFz1yQI2IQPM%253D 

答案1

得分: 1

在Retrofit2中,默认情况下参数名和值都会进行URL编码。要更改此行为,您需要添加encoded = true。

void createMyStuff(
     @Header("X-Signature") String authorization,
     @Query(value="nickname", encoded = true) String nickname,
     @Query("language") String language,
     Callback<MyAPIResponse> cb);
英文:

In Retrofit2 Parameter names and values are URL encoded by default. You need to add encoded = true to change this behaviour.

void createMyStuff(
     @Header(&quot;X-Signature&quot;) String authorization,
     @Query(value=&quot;nickname&quot;, encoded = true) String nickname,
     @Query(&quot;language&quot;) String language,
     Callback&lt;MyAPIResponse&gt; cb);

huangapple
  • 本文由 发表于 2020年7月29日 19:34:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63152701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定