2017-06-21 19 views
0

我有一個改造的對象是這樣的:發送兩個字符串項作爲身體改造

retrofit = new Retrofit.Builder() 
       .baseUrl(BASE_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 

@Headers 
({ 
    "Content-Type: application/x-www-form-urlencoded", 
      "cache-control: no-cache" 
}) 
@POST("login") 
Call<RegisterResponse> register(@Body String n1,@Body String n2); 

,我知道那是因爲兩個身上標註不正確。
所以我必須使用此代碼

@Headers 
({ 
    "Content-Type: application/x-www-form-urlencoded", 
      "cache-control: no-cache" 
}) 
@POST("login") 
Call<RegisterResponse> register(@Body TestObject testObject); 

class TestObject{ 
    String n1; 
    String n2; 

} 

,但我有我不能改變它,它得到兩個參數作爲身體的服務器。
當我使用郵遞員我的服務器工作得很好,做它應該做的。
但是當我使用改裝我得到一個錯誤500「服務器內部錯誤」
我與okhttp

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); 
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build(); 



    MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); 
    RequestBody body = RequestBody.create(mediaType, "n1=09369&n2=145616"); 
    Request request = new Request.Builder() 
      .url(URL) 
      .post(body) 
      .addHeader("content-type", "application/x-www-form-urlencoded") 
      .addHeader("cache-control", "no-cache") 
      .addHeader("postman-token", "0761ac45-1fb7-78df-d088-2437ecb984a3") 
      .build(); 

    okhttp3.Response response = client.newCall(request).execute(); 

及其作品不錯,但我怎麼能與改造做到這一點做到了這一點?

+0

'當我使用郵遞員我的服務器工作得很好'你到底有多使用它?你通過2個身體哪裏通過? –

+0

在郵差我只有一個身體標籤,我添加兩個參數在它 – max

+0

然後做同樣的 - 添加2個參數。沒有設置身體。 –

回答

1

你需要與@FormUrlEncoded註釋發送的數據是什麼,閱讀更多關於它here

你canuse這樣說:

@FormUrlEncoded 
@POST("login") 
Call<RegisterResponse> register(@Filed("n1") String n1, @Filed("n2") String n2); 
0

需要修改的位

class TestObject{ 
    @SerializedName("n1") 
    @Expose 
    String n1;//if your server is looking for key with name n1, or change it to the required key 

    @SerializedName("n2") 
    @Expose 
    String n2; 
} 

您的通話代碼將保持不變。

希望這可能會有幫助。:)

+0

不工作相同的錯誤:( – max

相關問題