Retrofit 2 编码中的特殊字符问题 [英] Issue with Special Characters in Retrofit 2 encoding
问题描述
所以我希望向我们的 api 发出请求以登录用户,但是有一个部分在 Retrofit 2 方法中被编码,即使它设置为 encoded = true
.基本网址是 https://testapi.test.ie
我作为 serverext
传递的参数是 mdc.php?action=
但是即使设置 encoded = true
后,结果请求正文为:https://testapi.test.ie/mdc.php%3Faction=login_user&ts=1482924232742
我需要的地方为:https://testapi.test.ie/mdc.php?action=login_user&ts=1482924232742
所以我可以看到问题是 ?
符号.以下是我的改造方法,如果有人可以提供帮助,我将不胜感激以实现正确的
So I'm looking to make a request to our api to log in a user, however there is a section that gets encoded in the Retrofit 2 method even though its set as encoded = true
. The base url is https://testapi.test.ie
The parameter I pass as the serverext
is mdc.php?action=
However even after setting encoded = true
the resulting request body is: https://testapi.test.ie/mdc.php%3Faction=login_user&ts=1482924232742
where I require it to be: https://testapi.test.ie/mdc.php?action=login_user&ts=1482924232742
So I can see the issue is the ?
symbol. Below is my retrofit method, if anyone can help with this I would appreciate it in order to achieve the correct
@retrofit2.http.POST("/{serverext}login_user&ts={timestamp}")
@retrofit2.http.Multipart
Call<LoginResponseModel> loginUser(@retrofit2.http.Path(value = "serverext", encoded = true) String server,
@retrofit2.http.Part(Constants.USER) String username,
@retrofit2.http.Part(Constants.PASS) String password,
@retrofit2.http.Path("timestamp") Long timestamp);
推荐答案
你用错了.路径就是路径,查询就是查询.您需要重写代码以单独使用它.
You use it incorrect. Path is path, Query is query. You need to rewrite your code to use this separately.
@retrofit2.http.POST("{serverext}")
@FormUrlEncoded
Call<LoginResponseModel> loginUser(@retrofit2.http.Path(value = "serverext", encoded = true) String server,
@retrofit2.http.Field(Constants.USER) String username,
@retrofit2.http.Field(Constants.PASS) String password,
@retrofit2.http.Query("timestamp") Long timestamp,
@retrofit2.http.Query("action") String action);
loginUser("mdc.php", username, pass, 42, "login_user")
这篇关于Retrofit 2 编码中的特殊字符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!