|
@@ -4,7 +4,6 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.Interceptor;
|
|
|
import okhttp3.Request;
|
|
|
import okhttp3.Response;
|
|
|
-import okhttp3.ResponseBody;
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
import java.io.IOException;
|
|
@@ -27,24 +26,11 @@ public class OkHttpLogInterceptor implements Interceptor {
|
|
|
Request request = chain.request();
|
|
|
|
|
|
long t1 = System.nanoTime();//请求发起的时间
|
|
|
- OkHttpLogInterceptor.log.info(String.format("发送请求 %s on %s%n%s",
|
|
|
- request.url(), chain.connection(), request.headers()));
|
|
|
-
|
|
|
+ OkHttpLogInterceptor.log.info("----------Start-OkHttp----------------");
|
|
|
Response response = chain.proceed(request);
|
|
|
-
|
|
|
long t2 = System.nanoTime();//收到响应的时间
|
|
|
+ OkHttpLogInterceptor.log.info("----------End-OkHttp:" + (t2 - t1) / 1e6d + "毫秒----------");
|
|
|
|
|
|
- //这里不能直接使用response.body().string()的方式输出日志
|
|
|
- //因为response.body().string()之后,response中的流会被关闭,程序会报错,我们需要创建出一
|
|
|
- //个新的response给应用层处理
|
|
|
- ResponseBody responseBody = response.peekBody(1024 * 1024);
|
|
|
-
|
|
|
-
|
|
|
- OkHttpLogInterceptor.log.info(String.format("接收响应: [%s] %n返回json:【%s】 %.1fms%n%s",
|
|
|
- response.request().url(),
|
|
|
- responseBody.string(),
|
|
|
- (t2 - t1) / 1e6d,
|
|
|
- response.headers()));
|
|
|
while (!response.isSuccessful() && retryNum < maxRetry) {
|
|
|
retryNum++;
|
|
|
OkHttpLogInterceptor.log.info("重试次数retryNum=" + retryNum);
|