Retrofit2.0+OkHttp打印Request URL(请求地址参数)
2021-06-12 08:04
标签:app src image ima 获取 日志 UI style get 学习了Retrofit中的拦截器功能:实现日志中打印请求头内容 Retrofit 2+ 是基于OKHttp进行封装的,那么也就是说想进行请求拦截然后进行打印出来的话,就必须要从OkHttp进行入手。 1,添加依赖包: 2,我这里是新建了一个方法进行创建并获取OkHttp 3,把获取到的OkHttp给Retrofit进行使用 效果图:我发起了三个请求。 Retrofit2.0+OkHttp打印Request URL(请求地址参数) 标签:app src image ima 获取 日志 UI style get 原文地址:http://www.cnblogs.com/zhujiabin/p/7288950.htmlcompile ‘com.squareup.okhttp3:logging-interceptor:3.1.2‘
private OkHttpClient getOkHttpClient() {
//日志显示级别
HttpLoggingInterceptor.Level level= HttpLoggingInterceptor.Level.BODY;
//新建log拦截器
HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.d("zcb","OkHttp====Message:"+message);
}
});
loggingInterceptor.setLevel(level);
//定制OkHttp
OkHttpClient.Builder httpClientBuilder = new OkHttpClient
.Builder();
//OkHttp进行添加拦截器loggingInterceptor
httpClientBuilder.addInterceptor(loggingInterceptor);
return httpClientBuilder.build();
}
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(AppConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(getOkHttpClient())//使用自己创建的OkHttp
.build();
文章标题:Retrofit2.0+OkHttp打印Request URL(请求地址参数)
文章链接:http://soscw.com/essay/93840.html