diff --git a/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPContext.java b/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPContext.java index 9fd722299ce..bbd9c66491f 100644 --- a/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPContext.java +++ b/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPContext.java @@ -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; @@ -31,6 +32,7 @@ class HTTPContext { private HTTPContext() { super(); mClient = new OkHttpClient(); + //mClient.interceptors().add(new LoggingInterceptor()); } public static HTTPContext getInstance() { @@ -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(); @@ -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; + } + } }