Skip to content

Instantly share code, notes, and snippets.

@bhdrkn
Created June 16, 2015 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhdrkn/1305f1b433c4af624f85 to your computer and use it in GitHub Desktop.
Save bhdrkn/1305f1b433c4af624f85 to your computer and use it in GitHub Desktop.
Okhttp-SystemOutInterceptor
package com.bahadirakin.api;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;
/**
* FIXME: Use logger instread of Sout
*
* TODO: okHttpClient.networkInterceptors().add(new LoggingInterceptor());
*/
public class LoggingInterceptor implements Interceptor {
private AtomicLong atomicLong = new AtomicLong(0L);
public Response intercept(Chain chain) throws IOException {
final long id = atomicLong.incrementAndGet();
Request request = chain.request();
printRequest(id, request);
Response response = chain.proceed(request);
printResponse(id, response);
return response;
}
private void printResponse(long id, Response response) {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\nResponse------------------------------");
stringBuilder.append("\nID: " + id);
stringBuilder.append("\nResponse-Code: ");
stringBuilder.append(response.code());
stringBuilder.append("\nMessage: ");
stringBuilder.append(response.message());
stringBuilder.append("\nMethod: ");
stringBuilder.append(response.request().method());
stringBuilder.append("\nAddress: ");
stringBuilder.append(response.request().urlString());
stringBuilder.append("\nHeaders: {\n\t");
stringBuilder.append(response.headers().toString().replace("\n", "\n\t"));
stringBuilder.append("}");
stringBuilder.append("\n--------------------------------------");
System.out.println(stringBuilder.toString());
}
private void printRequest(long id, Request request) {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\nRequest------------------------------");
stringBuilder.append("\nID: " + id);
stringBuilder.append("\nAddress: ");
stringBuilder.append(request.urlString());
stringBuilder.append("\nMethod: ");
stringBuilder.append(request.method());
if (request.body() != null) {
stringBuilder.append("\nContent-Type: " + request.body().contentType());
}
stringBuilder.append("\nHeaders: {\n\t");
stringBuilder.append(request.headers().toString().replace("\n", "\n\t"));
stringBuilder.append("}");
stringBuilder.append("\n--------------------------------------");
System.out.println(stringBuilder.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment