通过添加拦截器的方式,打印通信日志。
添加依赖库
implementation "com.squareup.okhttp3:logging-interceptor:+"
创建日志拦截器
private HttpLoggingInterceptor createHttpLogInterceptor() {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(
new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i(TAG, "" + message);
}
});
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return httpLoggingInterceptor;
}
添加日志拦截器
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(createHttpLogInterceptor())
.build();