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

时间:2016-02-08 09:14:19

标签: android retrofit android-networking retrofit2

我正在使用改造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监视器中,但其余的一切都被记录下来。

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'
}

由于报告错误问题Bug Link

,使用RC1和3.0.1

5 个答案:

答案 0 :(得分:18)

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

 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();

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

 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();

答案 1 :(得分:7)

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

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

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

答案 2 :(得分:4)

使用addInterceptor代替使用addNetworkInterceptor添加日志记录拦截器,以包含由OkHttp添加的标头。

Network interceptors能够:

  

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

答案 3 :(得分:0)

您需要设置以下内容:

   OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
   HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
   httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
   clientBuilder.addNetworkInterceptor(httpLoggingInterceptor);
   clientBuilder.build()

答案 4 :(得分:0)

以下链接非常有用: https://medium.com/swlh/okhttp-interceptors-with-retrofit-2dcc322cc3f3

OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();

//Gson Builder
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Timber.plant(new Timber.DebugTree());

// HttpLoggingInterceptor
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(message -> Timber.i(message));
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);


/**
 * injection of interceptors to handle encryption and decryption
 */

//Encryption Interceptor
EncryptionInterceptor encryptionInterceptor = new EncryptionInterceptor(new EncryptionImpl());
//Decryption Interceptor
DecryptionInterceptor decryptionInterceptor = new DecryptionInterceptor(new DecryptionImpl());


// OkHttpClient. Be conscious with the order
OkHttpClient okHttpClient = new OkHttpClient()
    .newBuilder()
    //httpLogging interceptor for logging network requests
    .addInterceptor(httpLoggingInterceptor)
    //Encryption interceptor for encryption of request data
    .addInterceptor(encryptionInterceptor)
    // interceptor for decryption of request data
    .addInterceptor(decryptionInterceptor)
    .build();

//Retrofit
Retrofit retrofit = new Retrofit.Builder()
    .client(okHttpClient)
    .baseUrl(BASE_URL)
    // for serialization
    .addConverterFactory(GsonConverterFactory.create(gson))
    .build();

//ApiService
apiService = retrofit.create(ApiService.class);