【问题标题】:How to retrieve JSON payload inside Http interceptor?如何在 Http 拦截器中检索 JSON 有效负载?
【发布时间】:2017-12-01 20:52:09
【问题描述】:

我试图在我的拦截器中拦截一个特定的请求。我想捕捉401 http状态,这很容易做到

response.code()

但是,我还需要检查负载中的特定字段,但我找不到如何检索此负载。

这是我的拦截器:

Interceptor authorizationInterceptor = new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        okhttp3.Response response = chain.proceed(request);

        if (response.code() == 401) {
            // I need to access the payload here
            return response;
        }

        return response;
   }
};

调试器显示有效负载在响应中可用。

能找回来吗?

【问题讨论】:

  • response.body().string() 呢?请记住,它将消耗响应的正文
  • @Selvin 这正是我所需要的,谢谢

标签: android retrofit2 okhttp3


【解决方案1】:

正如 Selvin 所建议的,可以使用

来检索正文(作为字符串)
response.body().string()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2018-07-08
    相关资源
    最近更新 更多