Last active
September 11, 2015 17:39
-
-
Save Avinash-Bhat/cc0dac9e842d44befdbd to your computer and use it in GitHub Desktop.
Revisions
-
Avinash-Bhat revised this gist
Sep 11, 2015 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,6 @@ OkHttpClient createClient() { final OkHttpClient client = new OkHttpClient(); // ... client.networkInterceptors().add(chain -> { Request request = chain.request(); if (DEBUG) { @@ -22,7 +23,7 @@ Client createClient() { } return response; }); // ... return client; } -
Avinash-Bhat revised this gist
Sep 11, 2015 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ Client createClient() { ... client.networkInterceptors().add(chain -> { Request request = chain.request(); if (DEBUG) { @@ -21,6 +22,8 @@ Client createClient() { } return response; }); ... return client; } void log(Request r) throws IOException { -
Avinash-Bhat created this gist
Sep 11, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,84 @@ Client createClient() { client.networkInterceptors().add(chain -> { Request request = chain.request(); if (DEBUG) { log(request); } else { String protocolPrefix = request.isHttps() ? "S" : ""; Log.i(TAG, String.format("---> HTTP%s %s %s", protocolPrefix, request.method(), request.urlString())); } long start = System.nanoTime(); Response response = chain.proceed(request); long elapsedTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); if (DEBUG) { return log(request.urlString(), response, elapsedTime); } else { Log.i(TAG, String.format("<--- HTTP %s %s (%sms)", response.code(), request.urlString(), elapsedTime)); } return response; }); } void log(Request r) throws IOException { final Request request = r.newBuilder().build(); String protocolPrefix = request.isHttps() ? "S" : ""; Log.d(TAG, String.format("---> HTTP%s %s %s", protocolPrefix, request.method(), request.urlString())); Headers headers = request.headers(); for (String header : headers.names()) { Log.d(TAG, String.format("%s: %s", header, headers.get(header))); } String bodySize = "no"; RequestBody body = request.body(); if (body != null) { MediaType bodyMime = body.contentType(); if (bodyMime != null) { Log.d(TAG, "Content-Type: " + bodyMime); } long bodyLength = body.contentLength(); bodySize = bodyLength + "-byte"; if (bodyLength != -1) { Log.d(TAG, "Content-Length: " + bodyLength); } if (headers.size() > 0) { Log.d(TAG, "\n"); } Buffer sink = new Buffer(); body.writeTo(sink); if (bodyMime != null) { Log.d(TAG, sink.readUtf8()); } } Log.d(TAG, String.format("---> END HTTP%s (%s body)", protocolPrefix, bodySize)); } private Response log(String url, Response response, long elapsedTime) throws IOException { Response.Builder builder = response.newBuilder(); Log.d(TAG, String.format("<--- HTTP %s %s (%sms)", response.code(), url, elapsedTime)); Headers headers = response.headers(); for (String header : headers.names()) { Log.d(TAG, String.format("%s: %s", header, headers.get(header))); } ResponseBody body = response.body(); byte[] source = body.bytes(); MediaType mediaType = body.contentType(); builder.body(ResponseBody.create(mediaType, source)); if (headers.size() > 0) { Log.d(TAG, ""); } Charset defaultCharset = Charset.forName("UTF-8"); Charset charset = mediaType != null ? mediaType.charset() != null ? mediaType.charset() : defaultCharset : defaultCharset; Log.d(TAG, new String(source, charset)); Log.d(TAG, String.format("<--- END HTTP (%s-byte body)", body.contentLength())); return builder.build(); }