【发布时间】:2016-02-08 09:14:19
【问题描述】:
我正在使用改造 2.x,我想记录请求和响应的标头和正文。
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.addNetworkInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder()
.addHeader("key", "value")
.addHeader("HEADER","HEADER Value")
.build();
return chain.proceed(request);
}
}).build();
这就是我的做法,我的问题是请求的标头没有被记录在 Android Monitor 中,但其余的一切都被记录了。
Gradle 版本
compile ('com.squareup.retrofit2:retrofit:2.0.0-beta3') {
// exclude Retrofit’s OkHttp peer-dependency module and define your own module import
exclude module: 'okhttp'
}
compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta3'
compile ('com.squareup.okhttp3:logging-interceptor:3.0.1'){
exclude module: 'okhttp'
}
使用 RC1 和 3.0.1 报告了错误问题 Bug Link
【问题讨论】:
-
您是否尝试过将日志拦截器移动到最后一个位置,即。 e.在添加标题的拦截器下方?根据文档:
...interceptors are called in order.也许响应只是在添加标头之前记录? -
@david.mihola : 我试过.. 但没用。
-
作为替代方案,您可以使用 facebook 的 stetho 库 facebook.github.io/stetho
标签: android retrofit android-networking retrofit2