How to log request and response body with Retrofit-Android?

前端 未结 9 2030
盖世英雄少女心
盖世英雄少女心 2020-11-28 21:35

I can\'t find relevant methods in the Retrofit API for logging complete request/response bodies. I was expecting some help in the Profiler (but it only offers meta-data abou

相关标签:
9条回答
  • 2020-11-28 22:05

    For android studio before 3.0 (using android motinor)
    https://futurestud.io/tutorials/retrofit-2-log-requests-and-responses
    https://www.youtube.com/watch?v=vazLpzE5y9M

    And for android studio from 3.0 and above (using android profiler as android monitor is replaced by android profiler)
    https://futurestud.io/tutorials/retrofit-2-analyze-network-traffic-with-android-studio-profiler

    0 讨论(0)
  • 2020-11-28 22:07

    Retrofit 2.0 :

    UPDATE: @by Marcus Pöhls

    Logging In Retrofit 2

    Retrofit 2 completely relies on OkHttp for any network operation. Since OkHttp is a peer dependency of Retrofit 2, you won’t need to add an additional dependency once Retrofit 2 is released as a stable release.

    OkHttp 2.6.0 ships with a logging interceptor as an internal dependency and you can directly use it for your Retrofit client. Retrofit 2.0.0-beta2 still uses OkHttp 2.5.0. Future releases will bump the dependency to higher OkHttp versions. That’s why you need to manually import the logging interceptor. Add the following line to your gradle imports within your build.gradle file to fetch the logging interceptor dependency.

    compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
    

    You can also visit Square's GitHub page about this interceptor

    Add Logging to Retrofit 2

    While developing your app and for debugging purposes it’s nice to have a log feature integrated to show request and response information. Since logging isn’t integrated by default anymore in Retrofit 2, we need to add a logging interceptor for OkHttp. Luckily OkHttp already ships with this interceptor and you only need to activate it for your OkHttpClient.

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();  
    // set your desired log level
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();   
    // add your other interceptors …
    // add logging as last interceptor
    httpClient.addInterceptor(logging);  // <-- this is the important line!
    Retrofit retrofit = new Retrofit.Builder()  
            .baseUrl(API_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClient.build())
            .build();
    

    We recommend to add logging as the last interceptor, because this will also log the information which you added with previous interceptors to your request.

    Log Levels

    Logging too much information will blow up your Android monitor, that’s why OkHttp’s logging interceptor has four log levels: NONE, BASIC, HEADERS, BODY. We’ll walk you through each of the log levels and describe their output.

    further information please visit : Retrofit 2 — Log Requests and Responses

    OLD ANSWER:

    no logging in Retrofit 2 anymore. The development team removed the logging feature. To be honest, the logging feature wasn’t that reliable anyway. Jake Wharton explicitly stated that the logged messages or objects are the assumed values and they couldn’t be proofed to be true. The actual request which arrives at the server may have a changed request body or something else.

    Even though there is no integrated logging by default, you can leverage any Java logger and use it within a customized OkHttp interceptor.

    further information about Retrofit 2 please refer : Retrofit — Getting Started and Create an Android Client

    0 讨论(0)
  • 2020-11-28 22:13

    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))
                    .addInterceptor(new OAuthInterceptor(context))
                    .authenticator(new BearerTokenAuthenticator(context))
                    .addInterceptor(loggingInterceptor)//at the end
                    .build();
    
    0 讨论(0)
提交回复
热议问题