简体   繁体   English

Retrofit 2编码中的特殊字符问题

[英]Issue with Special Characters in Retrofit 2 encoding

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 . 因此,我正在寻求向我们的api发出请求以登录用户的请求,但是,即使将Retrofit 2方法设置为encoded = true ,也有一部分会在Retrofit 2方法中进行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 ? 基本URL是https://testapi.test.ie 。我在serverext传递的参数是mdc.php?action=但是即使在将encoded = true设置encoded = true ,最终的请求正文仍是: https://testapi.test.ie/mdc.php%3Faction=login_user&ts=1482924232742 : mdc.php?action= https://testapi.test.ie/mdc.php%3Faction=login_user&ts=1482924232742我需要将其https://testapi.test.ie/mdc.php%3Faction=login_user&ts=1482924232742在以下位置: https://testapi.test.ie/mdc.php?action=login_user&ts=1482924232742 : https://testapi.test.ie/mdc.php%3Faction=login_user&ts=1482924232742所以我可以看到问题是? 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")

You need to use @FormUrlEncoded . 您需要使用@FormUrlEncoded And you don't need to include package name in all declarations! 而且您不需要在所有声明中都包含程序包名称! just import them! 只需导入它们! its more neat and clean! 它更干净整洁!

  @POST("/{serverext}login_user&ts={timestamp}")
    @Multipart
    @FormUrlEncoded
    Call<LoginResponseModel> loginUser(@Path(value = "server", encoded = true) String server,
                                       @Part(SyncStateContract.Constants.USER) String username,
                                       @Part(SyncStateContract.Constants.PASS) String password,
                                       @Path("timestamp") Long timestamp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM