英文:
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
"0wDs7cLFw63nXIQc%2FodiEJr0DWgJJJGj%2ByALAIaN%2F0TnYKp7yDYcKjE8Kftf%2Bg7tA68DMz5g%2FhJAfmKhzOKVESmrw0OUviTiFz1yQI2IQPM%3D"
but when I make the API call the same string becomes
"0wDs7cLFw63nXIQc%252FodiEJr0DWgJJJGj%252ByALAIaN%252F0TnYKp7yDYcKjE8Kftf%252Bg7tA68DMz5g%252FhJAfmKhzOKVESmrw0OUviTiFz1yQI2IQPM%253D"
Here is my code:
@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);
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("X-Signature") String authorization,
@Query(value="nickname", encoded = true) String nickname,
@Query("language") String language,
Callback<MyAPIResponse> cb);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论