【问题标题】:HttpLoggingInterceptor is not logging response for OkHttp body for some reason由于某种原因,HttpLoggingInterceptor 没有记录 OkHttp 正文的响应
【发布时间】:2016-03-31 02:46:35
【问题描述】:

我有来自 Square 的 OkHttpClient 2.7 和 HttpLoggingInterceptor 2.7。我创建了返回 json 的“GET”请求,但由于某种原因没有记录响应正文,响应正文的结果仅为“}”。控制台中存在所有其他内容,如标题、内容类型等。

HttpLoggHttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
mHttpClient.interceptors().add(interceptor);

问题应该出在那个字符串中: logger.log(buffer.clone().readString(charset));

--> END GET
<-- HTTP/1.1 200 OK (420ms)
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; odata.metadata=minimal; odata.streaming=true
Expires: -1
Server: Microsoft-IIS/8.5
OData-Version: 4.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 24 Dec 2015 17:51:42 GMT
Content-Length: 2291
OkHttp-Sent-Millis: 1450979490431
OkHttp-Received-Millis: 1450979490851
}

【问题讨论】:

    标签: android request retrofit interceptor okhttp


    【解决方案1】:

    似乎 JSON 响应字符串中的换行符是原因。替换

    logger.log(buffer.clone().readString(charset));
    

    与:

    String[] bufferSplit = buffer.clone().readString(charset).split("\\r?\\n");
    for (String s : bufferSplit) {
        logger.log(s);
    }
    

    这会给出完整的响应 JSON。我建议在发布版本中这样做,但对于开发它可以完成工作。

    【讨论】:

      猜你喜欢
      • 2021-04-13
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 2014-12-27
      • 1970-01-01
      相关资源
      最近更新 更多