okhttp日志拦截 package www.jc.house.common.http;import android.util.Log;import java.io.IOException;import java.nio.charset.Charset;import java.util.concurrent.TimeUnit;import okhttp3.Connection;import okhttp3.Headers;import okhttp3.Inte
package www.jc.house.common.http; import android.util.Log; import java.io.IOException; import java.nio.charset.Charset; import java.util.concurrent.TimeUnit; import okhttp3.Connection; import okhttp3.Headers; import okhttp3.Interceptor; import okhttp3.MediaType; import okhttp3.Protocol; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.ResponseBody; import okio.Buffer; /** * Created by xin on 2017/7/22. */ public class HttpLogSaveInterceptor implements Interceptor { private static final Charset UTF8 = Charset.forName("UTF-8"); @Override public Response intercept(Chain chain) throws IOException { //Request Request request = chain.request(); RequestBody requestBody = request.body(); boolean hasRequestBody = requestBody != null; Connection connection = chain.connection(); Protocol protocol = connection != null ? connection.protocol() : Protocol.HTTP_1_1; String requestStartMessage = "--> " + request.method() + ' ' + request.url() + ' ' + protocol; Log.v("http",requestStartMessage); if (hasRequestBody) { if (requestBody.contentType() != null) { Log.v("http", "Content-Type: " + requestBody.contentType()); } if (requestBody.contentLength() != -1) { Log.v("http", "Content-Length: " + requestBody.contentLength()); } } Headers headers = request.headers(); for (int i = 0, count = headers.size(); i < count; i++) { String name = headers.name(i); // Skip headers from the request body as they are explicitly logged above. if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Length".equalsIgnoreCase(name)) { Log.v("http", name + ": " + headers.value(i)); } } //请求参数 Buffer buffer = new Buffer(); requestBody.writeTo(buffer); Charset charset = UTF8; MediaType contentType = requestBody.contentType(); if (contentType != null) { charset = contentType.charset(UTF8); } Log.v("http", ""); Log.v("http", buffer.readString(charset)); Log.v("http", "--> END " + request.method() + " (" + requestBody.contentLength() + "-byte body)"); //Response long startNs = System.nanoTime(); Response response = chain.proceed(request); long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs); ResponseBody responseBody = response.body(); long contentLength = responseBody.contentLength(); String bodySize = contentLength != -1 ? contentLength + " -byte" : " unknown-length"; Log.v("http","<-- " + response.code() + ' ' + response.message() + ' ' + response.request().url() + " (" + tookMs + "ms" + bodySize + " body" ); Headers responseHeaders = response.headers(); for (int i = 0, count = headers.size(); i < count; i++) { Log.v("http",responseHeaders.name(i) + ": " + responseHeaders.value(i)); } okhttp3.MediaType mediaType = response.body().contentType(); String content = response.body().string(); Log.v("http", content); Log.v("http", "<-- END HTTP"); return response.newBuilder() .body(okhttp3.ResponseBody.create(mediaType, content)) .build(); } }