Logging interceptor
suggest changeInterceptors intercept OkHttp calls. A reason to intercept is to monitor, rewrite, or retry a call. That can apply to the outgoing request, the incoming response, or both. logger is not declared on this page.
class LoggingInterceptor implements Interceptor {
@Override public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
logger.info(String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
long t2 = System.nanoTime();
logger.info(String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));
return response;
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents