2016-12-28 37 views
0

因此,我正在尋求向我們的api請求登錄用戶,但是有一部分在改進2方法中編碼,即使其設置爲encoded = true。的基本網址爲https://testapi.test.ie我通過爲serverext的參數設置,即使在encoded = true導致請求主體是後mdc.php?action=但是:https://testapi.test.ie/mdc.php%3Faction=login_user&ts=1482924232742,我要求它:https://testapi.test.ie/mdc.php?action=login_user&ts=1482924232742所以我能看到的問題是?符號。下面是我的改造方法,如果任何人都可以在這方面幫助我將不勝感激,以達到正確的改進2編碼中的特殊字符問題

@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); 

回答

1

您使用不正確。路徑是路徑,查詢是查詢。您需要重寫您的代碼以單獨使用此代碼。

@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") 
+0

如果我更改爲這種格式,則請求形成正確,但我收到來自API的錯誤響應,因爲無效憑證不正確,因爲憑證是正確的。我認爲它需要@ Path而不是@ Query。我從1.9升級,它是@ Path並正常工作。這種編碼特殊字符是retrofit2還是okhttp3的限制? –

+0

我不知道編碼,但你用API不正確。你能檢查你的要求嗎?什麼是不正確的? –

+0

我目前正在從1.9升級到2.0,在1.9我們使用了像這樣的@pi(「/ {server} login_user&ts = {timestamp}」) @Multipart void loginUser(@Path(value =「server」, (Constants.USER)字符串用戶名, @Part(Constants.PASS)字符串密碼, @Path(「timestamp」)長時間戳,回調回調);'這工作正確地進行測試時,服務器的測試結果是憑據是正確的。 –

0

您需要使用@FormUrlEncoded。而且您不需要在所有聲明中包括包名稱!只需導入它們!它更整潔乾淨!

@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); 
+0

嗨,軟件包名稱聲明是允許我從1.9升級到Retrofit2,目前我正在執行此過程。我已經嘗試過@ @FormUrlEncoded,它不起作用,我仍然用''編碼。感謝您的幫助,您有任何其他建議嗎? –

+0

有沒有人有關於此的任何信息?至於爲什麼當我設置'encoded = true'時,'?'仍然被編碼? –

+0

如果你不想要你的?被編碼爲什麼你做了'encoded = true'?只是離開或使其'假' –