我们知道,Retrofit是基于OkHttp发展而来,因此在Retrofit中设置日志打印,就是在OkHttp设置。
1、添加依赖
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
2、添加打印代码
HttpLoggingInterceptor:http日志拦截器
方法1:自定义输出日志格式
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
try {
String text = URLDecoder.decode(message, "utf-8");
Log.e("OKHttp-----", text);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e("OKHttp-----", message);
}
}
});
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
// 四个级别:NONE,BASIC,HEADER,BODY
// BASEIC:请求/响应行
// HEADER:请求/响应行 + 头
// BODY:请求/响应航 + 头 + 体
mClient = new OkHttpClient.Builder().addInterceptor(interceptor).build();
方法2:默认打印输出如下
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLogg