package com.igo.user.network; import android.util.Log; import java.io.IOException; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import okhttp3.Interceptor; import okhttp3.MediaType; import okhttp3.MultipartBody; import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; import okio.Buffer; public class LoggingInterceptor implements Interceptor { private static final String TAG = "zmc-Log"; private volatile Level level = Level.NONE; public Level getLevel() { return level; } public void setLevel(Level l) { level = l; } public enum Level { NONE, BODY } @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); long startTime = System.currentTimeMillis(); Log.d(TAG + "-" + Thread.currentThread().getId(), "----------Start----------------"); if ("GET".equalsIgnoreCase(request.method()) || "DELETE".equalsIgnoreCase(request.method())) { Log.d(TAG + "-" + Thread.currentThread().getId(), "| " + URLDecoder.decode(request.toString(), StandardCharsets.UTF_8.name())); } else { String requestInfo = "Request{method=" + request.method() + ", url=" + request.url() + ", head=" + request.headers() + "}"; Log.d(TAG + "-" + Thread.currentThread().getId(), "| " + requestInfo); try { String jsonInfo = "RequestParams:" + URLDecoder.decode(bodyToString(request), StandardCharsets.UTF_8.name()); Log.d(TAG + "-" + Thread.currentThread().getId(), "| " + jsonInfo); } catch (Exception e) { Log.e(TAG + "-" + Thread.currentThread().getId(), "| " + "RequestParams:" + bodyToString(request)); Log.e(TAG + "-" + Thread.currentThread().getId(), "| " + "error:" + e.getMessage()); } } Response response = chain.proceed(request); long endTime = System.currentTimeMillis(); String bodyString = response.body().string(); Log.d(TAG + "-" + Thread.currentThread().getId(), "| Response:" + bodyString); long duration = endTime - startTime; Log.d(TAG + "-" + Thread.currentThread().getId(), "----------End:" + duration + "毫秒----------"); ResponseBody newBody = ResponseBody.create(response.body().contentType(), bodyString); return response.newBuilder() .body(newBody) .build(); } private String bodyToString(Request request) { try { final Request copy = request.newBuilder().build(); final Buffer buffer = new Buffer(); if (copy.body() instanceof MultipartBody) { MultipartBody multipartBody = (MultipartBody) copy.body(); for (int i = 0; i < multipartBody.size(); i++) { final MultipartBody.Part part = multipartBody.part(i); MediaType mediaType = part.body().contentType(); if (!isFilePart(mediaType)) { part.body().writeTo(buffer); } else { buffer.writeUtf8("file params Content-Type: " + mediaType + "\n"); } } } else { copy.body().writeTo(buffer); } return buffer.readUtf8(); } catch (IOException e) { return "did not work"; } } // 是否为文件 boolean isFilePart(MediaType mediaType) { if (mediaType != null) { String type = mediaType.toString(); return type.startsWith("audio/") || type.startsWith("text/plain"); } return false; } }