Retrofit 2.x:请求和响应的日志标头

Cod*_*ife 12 android android-networking retrofit retrofit2

我正在使用retrofit 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();
Run Code Online (Sandbox Code Playgroud)

这就是我正在做的,我的问题是请求的标题没有记录在Android监视器,但休息一切都记录下来.

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'
}
Run Code Online (Sandbox Code Playgroud)

由于bug问题使用RC1和3.0.1报告了Bug Link

die*_*per 23

哦,如果有人有兴趣,我发现错误:

 HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
        .addInterceptor(interceptor)
        .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
        .addInterceptor(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();
Run Code Online (Sandbox Code Playgroud)

您必须在请求拦截器之后添加日志拦截器(您的拦截器变量),因此正确的答案是:

 HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(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);
            }


        })
        .addInterceptor(interceptor)
        .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
        .build();
Run Code Online (Sandbox Code Playgroud)

  • 疯狂的是它没有明确说明这一点或者它没有处理所有订单......谢谢!:) (3认同)

Cod*_*ife 9

可以帮助别人......

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
Run Code Online (Sandbox Code Playgroud)

添加两个以查看完整日志并在最后添加此拦截器(不知道为什么,但它像这样).

  • 你不需要添加它们,它基本上没用.设置`BODY`只应显示所有标题和正文日志.https://github.com/square/okhttp/blob/master/okhttp-logging-interceptor/src/main/java/okhttp3/logging/HttpLoggingInterceptor.java#L132 (2认同)

rea*_*una 5

不用addInterceptor添加日志记录拦截器,而是使用addNetworkInterceptor来包含OkHttp添加的标头。

网络拦截器能够:

观察数据,就像通过网络传输数据一样。

  • 使用 addNetworkInterceptor 对我的情况有帮助。谢谢 (3认同)