如何使用Retrofit 2.0获取原始响应和请求

时间:2016-05-07 13:11:33

标签: java android retrofit retrofit2 okhttp

我正在尝试使用Retrofit2.0.2获取原始响应。

到目前为止,我尝试使用以下代码行打印响应,但它会打印地址而不是确切的响应正文。

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

                }
            });

4 个答案:

答案 0 :(得分:7)

那是因为它已经被转换器转换为对象。要获取原始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();

不要忘记在Retrofit中更改您的HttpClient。

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

在Log Cat中,您将看到原始json响应。有关详细信息,请参阅Square OkHttp github

<强>注意!

不要忘记在生产中删除拦截器(或将记录级别更改为NONE)!否则,人们将能够在Log Cat上看到您的请求和响应。

答案 1 :(得分:3)

只需使用:

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

或者:

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

答案 2 :(得分:2)

您必须在通话中使用okhttp3中的“ResponseBody”。然后,获取“response.body()。string()”以获取服务器提供给您的JSONObject。 如果解析服务器对模型对象的响应有任何错误,这是捕获错误的好方法。

答案 3 :(得分:1)

猜猜你想看到原始响应体以进行调试。有两种方法可以做到这一点。

  1. 使用okhttp3.logging.HttpLoggingInterceptor作为@aldok提及。

  2. 如果要检查响应对象的某些属性,或者手动将json(响应体)转换为POJO,您可能希望这样做:

  3. 在初始化改装客户端时不要使用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) {
    
            }
        });
    

    希望这会有所帮助〜

相关问题