【问题标题】:How to get raw response and request using Retrofit 2.0如何使用 Retrofit 2.0 获取原始响应和请求
【发布时间】:2016-09-02 12:09:40
【问题描述】:

我正在尝试使用 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);

                }
            });

【问题讨论】:

标签: java android retrofit retrofit2 okhttp


【解决方案1】:

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

小心!

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

【讨论】:

【解决方案2】:

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

【讨论】:

  • 这行得通。但是,您需要调用 response.string() 而不是 response.body().string()。
【解决方案3】:

简单使用:

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

或者:

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

【讨论】:

  • response.raw().body().string());给我无法读取转换后的正文的原始响应正文,因为您只能读取一次响应,因为它是一个流。您正在 UtilityMethods.convertResponseToString 方法中阅读它,因此您需要创建一个具有相同内容的新响应。
  • >> 您需要创建一个具有相同内容的新响应。我该怎么做?
【解决方案4】:

猜测您想查看原始响应正文以进行调试。有两种方法可以做到这一点。

  1. 使用@aldok 提到的okhttp3.logging.HttpLoggingInterceptor

  2. 如果你想检查响应对象的一些属性,或者手动将 json(response body) 转换为 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) {

        }
    });

希望对你有帮助~

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-21
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 2019-10-26
    • 2020-09-01
    相关资源
    最近更新 更多