Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
added application interceptor to help with debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
zugaldia authored and Leith Bade committed Nov 12, 2015
1 parent a950128 commit 486cc8e
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
Expand All @@ -31,6 +32,7 @@ class HTTPContext {
private HTTPContext() {
super();
mClient = new OkHttpClient();
//mClient.interceptors().add(new LoggingInterceptor());
}

public static HTTPContext getInstance() {
Expand Down Expand Up @@ -94,6 +96,8 @@ public void onFailure(Request request, IOException e) {

@Override
public void onResponse(Response response) throws IOException {
Log.d(LOG_TAG, "onResponse");

byte[] body;
try {
body = response.body().bytes();
Expand All @@ -107,4 +111,30 @@ public void onResponse(Response response) throws IOException {
nativeOnResponse(mNativePtr, response.code(), response.message(), response.header("ETag"), response.header("Last-Modified"), response.header("Cache-Control"), response.header("Expires"), body);
}
}

/*
* Application interceptor that logs the outgoing request and the incoming response.
* Based on https://github.com/square/okhttp/wiki/Interceptors
*/

class LoggingInterceptor implements Interceptor {

private final static String LOG_TAG = "LoggingInterceptor";

@Override public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();

long t1 = System.nanoTime();
Log.i(LOG_TAG, String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));

Response response = chain.proceed(request);

long t2 = System.nanoTime();
Log.i(LOG_TAG, String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));

return response;
}
}
}

0 comments on commit 486cc8e

Please sign in to comment.