我遇到了奇怪的问题。 OkHttp 给我这样的日志:
I/System.out: [OkHttp] sendRequest>>
I/System.out: [OkHttp] sendRequest<<
但是我在客户端初始化时声明了日志记录拦截器。
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
OkHttpClient.Builder client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder builder = original.newBuilder()
.header("Authorization", Credentials.basic(config.login(), config.password()));
Request request = builder.build();
return chain.proceed(request);
})
.addInterceptor(httpLoggingInterceptor);
return new Retrofit.Builder()
.baseUrl(config.getServer())
.callbackExecutor(Executors.newSingleThreadExecutor())
.addConverterFactory(getMoshiConverterFactory())
.client(client.build())
.build();
我的 gradle 包含:
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-moshi:2.3.0'
implementation 'com.squareup.moshi:moshi:1.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.7.0'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.7.0'
我做错了什么吗?
最佳答案
您可以将它用作网络拦截器而不是应用程序拦截器。执行 .addNetworkInterceptor(httpLoggingInterceptor);
而不是 .addInterceptor(httpLoggingInterceptor);
这可以让您“观察数据,就像数据将通过网络传输一样”。
查看更多信息in the wiki pages.
关于android - OkHttp 忽略日志记录拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51023415/