2016-05-07 128 views
8

我正在嘗試使用Retrofit2.0.2獲取原始響應。如何使用Retrofit 2.0獲取原始響應和請求

到目前爲止,我嘗試使用下面的代碼行打印響應,但它打印的地址,而不是確切的響應正文。

Log.i(「RAW MESSAGE」,response.body()。toString());

compile 'com.squareup.retrofit2:retrofit:2.0.2'

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


      GitApi gitApi = retrofit.create(GitApi.class); 

      Call<Addresses> call = gitApi.getFeed(user); 

    call.enqueue(new Callback<Addresses>() { 

       @Override 
       public void onResponse(Response<Addresses> response, Retrofit retrofit) { 
        try { 
         mDisplayDetails.setText(response.body().getSuburbs().get(0).getText()); 

        **Log.i("RAW MESSAGE",response.body().toString());** 

        } catch (Exception e) { 
         mDisplayDetails.setText(e.getMessage()); 
        } 
        mProgressBar.setVisibility(View.INVISIBLE); 

       } 

       @Override 
       public void onFailure(Throwable t) { 
        mDisplayDetails.setText(t.getMessage()); 
        mProgressBar.setVisibility(View.INVISIBLE); 

       } 
      }); 
+0

的可能的複製[改裝2.0b2:如何從響應得到的InputStream(HTTP:/ /stackoverflow.com/questions/33030137/retrofit-2-0b2-how-to-get-inputstream-from-the-response) –

+0

的人來到這裏,完整的答案可以在這裏找到:http://stackoverflow.com/問題/ 21398598/how-to-post-raw-whole-json-in-the-the-body-of-a-retrofit-request/36821182#36821182 – TommySM

回答

1

只需使用:

Log.i("RAW MESSAGE", response.raw().body().string()); 

或者:

Log.i("RAW MESSAGE", response.body().string()); 
+0

response.raw()。body()。string());給我無法讀取轉換體的原始響應體,因爲您只能讀取一次響應,因爲它是一個流。您正在使用UtilityMethods.convertResponseToString方法讀取它,因此您需要使用相同的內容創建一個新的Response。 – user3871754

+0

>>您需要使用相同的內容創建新的Response。我該怎麼做? – anivaler

4

那是因爲它是由轉換器已經轉換爲對象。要得到原始的json,你需要在你的Http客戶端上有一個攔截器。謝天謝地,您不需要編寫自己的類,Square已經爲您提供了HttpLoggingInterceptor類。

添加這對您的應用程序級gradle這個

compile 'com.squareup.okhttp3:logging-interceptor:3.5.0' 

而且用它在你的OkHttpClient

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

不要忘記改變你的HttpClient的改造。

Retrofit retrofit = new Retrofit.Builder() 
      .client(client)    
      .baseUrl("https://yourapi.com/api/") 
      .build(); 

在Log Cat中,您將看到原始的json響應。更多信息請參閱Square的OkHttp github

注意!

不要忘記在生產中刪除攔截器(或將日誌記錄級別更改爲NONE)!否則,人們將能夠在Log Cat上看到您的請求和響應。

+0

如何獲取最新版本的攔截器?我找不到它在廣場的github上 – zihadrizkyef

+1

@zihadrizkyef 3.8.1,你幾乎可以找到所有mvnrepository依賴,例如https://mvnrepository.com/artifact/com.squareup.okhttp3/logging-interceptor – aldok

2

您必須在您的通話中使用okhttp3中的「ResponseBody」。然後,獲取「response.body()。string()」來獲取服務器提供給您的JSONObject。 如果在解析模型對象的服務器響應時出現任何錯誤,這是一種很好的捕獲錯誤的方法。

+0

我試着同樣的方式,但它不起作用 – Erum

+1

這工作。但是,您需要調用response.string()而不是response.body()。string()。 –

0

爲了調試目的,猜測你想查看原始響應體。有兩種方法可以做到這一點。

  1. 使用okhttp3.logging.HttpLoggingInterceptor as @aldok提到。

  2. 如果你想檢查響應對象的某些屬性,或轉換成JSON(響應體)手動POJO,你可能想要做這樣的:

不要使用addConverterFactory,同時初始化改造客戶端,註釋這一行。

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

然後消耗這樣的迴應:

Call<ResponseBody> topRatedResponseCall = apiService.getTopRatedMoves(API_KEY); 
    topRatedResponseCall.enqueue(new Callback<ResponseBody>() { 
     @Override 
     public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
      try { 
       Log.d(LOG_TAG, response.body().string()); 
       int code = response.code(); 
       testText.setText(response.body().string()); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     public void onFailure(Call<ResponseBody> call, Throwable t) { 

     } 
    }); 

希望這將有助於〜