- 新建类自定义拦截器
package com.example.okhttp;
import android.util.Log;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
public class LogIntercept implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request=chain.request();
long curTime =System.currentTimeMillis();
Log.i("TAG","intercept: REQUEST="+ request.toString());
Response response=chain.proceed(request);
Log.i("TAG","intercept: REQUEST="+ response.toString());
Log.i("TAG","intercept:耗时="+(System.currentTimeMillis() - curTime) + "ms");
return response;
}
}
- 添加到MainActivity
okHttpclient = new OkHttpClient.Builder()
.addInterceptor(new LogIntercept())
.build();
标签:Log,chain,request,基础,okhttp3,intercept,使用,okhttp,import
From: https://www.cnblogs.com/dogleftover/p/18343841