android 日誌攔截器
阿新 • • 發佈:2018-11-09
package com.ppmoney.jde.common.http; import android.util.Log; import java.io.IOException; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; public class LoggingInterceptor implements Interceptor { @Override public Response intercept(Interceptor.Chain chain) throws IOException { //chain包含request和response Request request = chain.request(); //請求發起的時間 long t1 = System.nanoTime(); Log.d("-->", String.format("%s%n%s", request.url(), request.headers())); Response response = chain.proceed(request); //響應的時間 long t2 = System.nanoTime(); //建立一個response給應用層處理 response.headers()) ResponseBody responseBody = response.peekBody(1024 * 1024); Log.d("<--", String.format("%s %njson:%s %.1fms%n", response.request().url(), responseBody.string(), (t2 - t1) / 1e6d)); return response; } }