简体   繁体   中英

HttpLoggingInterceptor is not logging response for OkHttp body for some reason

I have OkHttpClient 2.7 with HttpLoggingInterceptor 2.7 from Square. I have created "GET" request that returns json but for some reason the response body is not logged and the result for the response body is only "}". All other stuff as header, content-type, etc is present in the console.

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

The problem should be in that string: 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
}

It seems newlines in a JSON response string are the cause. Replace

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

with:

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

This gives the full response JSON. I would not recommend doing this in a release build but for development it gets the job done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM