HttpLoggingInterceptor

HttpLoggingInterceptor

我有来自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
}

最佳答案

似乎是JSON响应字符串中的换行符。更换

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


与:

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


这给出了完整的响应JSON。我不建议在发行版本中进行此操作,但对于开发它可以完成工作。

10-04 11:43