【问题标题】:Retrofit 2.x : Log Header for request and responseRetrofit 2.x:请求和响应的日志头
【发布时间】: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

【问题讨论】:

标签: android retrofit android-networking retrofit2


【解决方案1】:

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

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

【讨论】:

  • 疯狂的是它没有明确说明这一点,或者它没有处理所有订单......谢谢! :)
【解决方案2】:

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

Network interceptors 能够:

观察将通过网络传输的数据。

【讨论】:

  • 感谢您分享这个。
  • 即使在此之后我也无法查看添加到请求中的自定义标头
  • 使用 addNetworkInterceptor 对我有帮助。谢谢
【解决方案3】:

它可以帮助某人......

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

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

【讨论】:

【解决方案4】:

您需要设置以下内容:

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

【讨论】:

    【解决方案5】:

    以下链接非常有用: 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);
    

    【讨论】:

      【解决方案6】:

      如果没有添加,则需要添加此库。

      实现 'com.squareup.okhttp3:logging-interceptor:4.7.2

      对所有 Log 使用级别为 Body 的记录器拦截器。

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

      并将其添加到您的 http 客户端。默认级别为 NONE。 仅供参考

      enum class Level {
          /** No logs. */
          NONE,
      
          /**
           * Logs request and response lines.
           *
           * Example:
           * ```
           * --> POST /greeting http/1.1 (3-byte body)
           *
           * <-- 200 OK (22ms, 6-byte body)
           * ```
           */
          BASIC,
      
          /**
           * Logs request and response lines and their respective headers.
           *
           * Example:
           * ```
           * --> POST /greeting http/1.1
           * Host: example.com
           * Content-Type: plain/text
           * Content-Length: 3
           * --> END POST
           *
           * <-- 200 OK (22ms)
           * Content-Type: plain/text
           * Content-Length: 6
           * <-- END HTTP
           * ```
           */
          HEADERS,
      
          /**
           * Logs request and response lines and their respective headers and bodies (if present).
           *
           * Example:
           * ```
           * --> POST /greeting http/1.1
           * Host: example.com
           * Content-Type: plain/text
           * Content-Length: 3
           *
           * Hi?
           * --> END POST
           *
           * <-- 200 OK (22ms)
           * Content-Type: plain/text
           * Content-Length: 6
           *
           * Hello!
           * <-- END HTTP
           * ```
           */
          BODY
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-29
        • 1970-01-01
        • 2017-08-07
        • 1970-01-01
        • 2016-07-02
        相关资源
        最近更新 更多