Last active
November 20, 2025 13:00
-
-
Save erickok/e371a9e0b9e702ed441d to your computer and use it in GitHub Desktop.
Simple logging interceptor for OkHttp that logs full request headers and response headers + body (useful for use with Retrofit 2 where logging was removed)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (BuildConfig.DEBUG) { | |
| httpclient.interceptors().add(new LoggingInterceptor()); | |
| } | |
| public class LoggingInterceptor implements Interceptor { | |
| @Override | |
| public Response intercept(Chain chain) throws IOException { | |
| Request request = chain.request(); | |
| long t1 = System.nanoTime(); | |
| Log.d("OkHttp", String.format("--> Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers())); | |
| Buffer requestBuffer = new Buffer(); | |
| request.body().writeTo(requestBuffer); | |
| Log.d("OkHttp", requestBuffer.readUtf8()); | |
| Response response = chain.proceed(request); | |
| long t2 = System.nanoTime(); | |
| Log.d("OkHttp", String.format("<-- Received response for %s in %.1fms%n%s", response.request().url(), (t2 - t1) / 1e6d, response.headers())); | |
| MediaType contentType = response.body().contentType(); | |
| BufferedSource buffer = Okio.buffer(new GzipSource(response.body().source())); | |
| String content = buffer.readUtf8(); | |
| Log.d("OkHttp", content); | |
| ResponseBody wrappedBody = ResponseBody.create(contentType, content); | |
| return response.newBuilder().removeHeader("Content-Encoding").body(wrappedBody).build(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (BuildConfig.DEBUG) { | |
| httpclient.interceptors().add(new LoggingInterceptor()); | |
| } | |
| public class LoggingInterceptor implements Interceptor { | |
| @Override | |
| public Response intercept(Chain chain) throws IOException { | |
| Request request = chain.request(); | |
| long t1 = System.nanoTime(); | |
| Log.d("OkHttp", String.format("--> Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers())); | |
| Buffer requestBuffer = new Buffer(); | |
| request.body().writeTo(requestBuffer); | |
| Log.d("OkHttp", requestBuffer.readUtf8()); | |
| Response response = chain.proceed(request); | |
| long t2 = System.nanoTime(); | |
| Log.d("OkHttp", String.format("<-- Received response for %s in %.1fms%n%s", response.request().url(), (t2 - t1) / 1e6d, response.headers())); | |
| MediaType contentType = response.body().contentType(); | |
| String content = response.body().string(); | |
| Log.d("OkHttp", content); | |
| ResponseBody wrappedBody = ResponseBody.create(contentType, content); | |
| return response.newBuilder().body(wrappedBody).build(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nah, time to just use hte one that comes with okhttp3