本文介绍了在没有 OkHttp 拦截的情况下使用 Retrofit2 进行日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在不使用 okHttp 库的情况下记录retrofit2 请求的完整请求(url、请求参数、正文).怎么办
I want to log the complete request (url, request param, body) of retrofit2 request without using okHttp Library. How can it be done
推荐答案
你必须添加 logging-interceptor:
You have to add logging-interceptor:
编译'com.squareup.okhttp3:logging-interceptor:3.4.1'
编译'com.squareup.retrofit2:retrofit:2.1.0'
OkHttpClient.Builder builder = new OkHttpClient.Builder();
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.networkInterceptors().add(httpLoggingInterceptor);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl(NetworkConstsParkCloud.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
没有捷径,没有 okhttp 就不能使用改造,从 4.4 开始,Android 上的 HttpUrlConnection 无论如何都在幕后使用 OkHttp:https://github.com/google/agera/issues/22
there are no shortcuts, you can not use retrofit without okhttp, As of 4.4, HttpUrlConnection on Android uses OkHttp under the hood anyway:https://github.com/google/agera/issues/22
这篇关于在没有 OkHttp 拦截的情况下使用 Retrofit2 进行日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!