public class LoggingInterceptor implements Interceptor {
String TAG = "mmm";
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
long startTime = System.currentTimeMillis();
Response response = chain.proceed(chain.request());
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
ResponseBody responseBody = response.body();
if (responseBody == null) {
return response;
}
okhttp3.MediaType mediaType = responseBody.contentType();
String content = response.body().string();
Log.e(TAG, "请求地址: " + request.toString());
Log.e(TAG, "请求状态: " + response.code() + " : " + response.message());
String method = request.method();
if ("POST".equals(method)) {
Buffer buffer = new Buffer();
try {
request.body().writeTo(buffer);
Charset charset = Charset.forName("UTF-8");
MediaType contentType = request.body().contentType();
if (contentType != null) {
charset = contentType.charset(UTF_8);
}
String params = buffer.readString(charset);
Log.e(TAG, "请求参数: " + params);
} catch (IOException e) {
e.printStackTrace();
}
}
Log.e(TAG, "请求结果: " + content);
Log.e(TAG, "==================================请求耗时: " + duration + "毫秒===================================");
return response.newBuilder().body(okhttp3.ResponseBody.create(mediaType, content)).build();
}
}
网友评论