Skip to content

Instantly share code, notes, and snippets.

@javadghane
Created December 28, 2016 05:43
Show Gist options
  • Save javadghane/3963b8c72a3d049de19b6c64a4f9e947 to your computer and use it in GitHub Desktop.
Save javadghane/3963b8c72a3d049de19b6c64a4f9e947 to your computer and use it in GitHub Desktop.
How to log all retrofit event?
Here a way that use Logging Interceptor from okhttp
@javadghane
Copy link
Author

add Logging Interceptor library:

compile 'com.squareup.okhttp3:logging-interceptor:find last version here'

like that:

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

@javadghane
Copy link
Author

add interceptor to your retrofit instance:

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();```
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
        
          retrofit = new Retrofit.Builder()
                .baseUrl(baseAddress)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client) //add here
                .build();

@javadghane
Copy link
Author

javadghane commented Dec 28, 2016

now all network log has been auto generated at logcat by this tag: 'OkHttp'

example:

D/dalvikvm: threadid=12 (OkHttp Dispatcher): calling run()
D/OkHttp: --> POST https://api.test.com/file.php http/1.1
D/OkHttp: Content-Type: application/x-www-form-urlencoded
D/OkHttp: Content-Length: 136
D/OkHttp: op=login&username=john&password=123
D/OkHttp: --> END POST (136-byte body)
D/dalvikvm: threadid=13 (OkHttp ConnectionPool): calling run()
D/dalvikvm: threadid=14 (OkHttp Dispatcher): calling run()
D/dalvikvm: threadid=16 (OkHttp ConnectionPool): calling run()
D/OkHttp: <-- HTTP FAILED: java.net.SocketTimeoutException: failed to connect to  https://api.test.com/file.php (port 443) after 10000ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment