Adding header to all request with Retrofit 2

前端 未结 10 1426
北荒
北荒 2020-11-29 17:46

Retrofit 2\'s documentation says:

Headers that need to be added to every request can be specified using an OkHttp interceptor.

I

10条回答
  •  一个人的身影
    2020-11-29 18:17

    If you use addInterceptor method for add HttpLoggingInterceptor, it won't be logging the things that added by other interceptors applied later than HttpLoggingInterceptor.

    For example: If you have two interceptors "HttpLoggingInterceptor" and "AuthInterceptor", and HttpLoggingInterceptor applied first, then you can't view the http-params or headers which set by AuthInterceptor.

    OkHttpClient.Builder builder = new OkHttpClient.Builder()
    .addNetworkInterceptor(logging)
    .addInterceptor(new AuthInterceptor());
    

    I solved it, via using addNetworkInterceptor method.

提交回复
热议问题