Logging with Retrofit 2

前端 未结 21 1604
春和景丽
春和景丽 2020-11-22 07:33

I\'m trying to get the exact JSON that is being sent in the request. Here is my code:

OkHttpClient client = new OkHt         


        
21条回答
  •  不知归路
    2020-11-22 07:52

    If you are using Retrofit2 and okhttp3 then you need to know that Interceptor works by queue. So add loggingInterceptor at the end, after your other Interceptors:

    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            if (BuildConfig.DEBUG)
                loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
    
     new OkHttpClient.Builder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .addInterceptor(new CatalogInterceptor(context))//first
                    .addInterceptor(new OAuthInterceptor(context))//second
                    .authenticator(new BearerTokenAuthenticator(context))
                    .addInterceptor(loggingInterceptor)//third, log at the end
                    .build();
    

提交回复
热议问题