该工具类用于打印请求参数等日志,若下载文件时,需要关闭该日志打印,否则会导致文件临时缓存,影响文件下载进度的获取,文件过大时还会导致OOM
import android.util.Log;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
public class RetrofitUtils {
/**
* 获取OkHttpClient
* 用于打印请求参数
*
* @return OkHttpClient
*/
public static OkHttpClient getOkHttpClient() {
// 日志显示级别
HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
// 新建log拦截器
HttpLoggingInterceptor loggingInterceptor =new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("Http请求参数:", message);
}
});
loggingInterceptor.setLevel(level);
// 定制OkHttp
OkHttpClient.Builder httpClientBuilder =new OkHttpClient.Builder();
// OkHttp进行添加拦截器loggingInterceptor
httpClientBuilder.addInterceptor(loggingInterceptor);
return httpClientBuilder.build();
}
}
网友评论