Monitoring and Intercepting Retrofit/OkHTTP Network Calls and Requests Using Chuck Interceptor

We all know how awesome Retrofit is and how simple it is to set up retrofit for making network requests. Retrofit is powered mainly by OKHttp.
People often ask the difference between OkHTTP and Retrofit, seeing that Retrofit uses OkHTTP under the hood. this StackOverflow answer does justice to that question.

So my question is what is exactly Retrofit for?
It is for generating the URL (using type-aware generated code tied to your specific REST API) and actually parsing the JSON using Gson.

Also, for certain types of REST operations (e.g. POST), it helps a bit in assembling what to submit (e.g. generating the encoded form).

Retrofit needs to perform HTTP operations. It uses OkHttp where available, for all that OkHttp provides: HTTP/2 and SPDY support, pluggable interceptors, etc.

CommonsWare

So what exactly is Chuck Interceptor and how does it come in?

As per the official documentation, Chuck Interceptor is a simple in-app HTTP inspector for Android OkHttp clients. Chuck intercepts and persists all HTTP requests and responses inside your application, and provides a UI for inspecting their content.

Apps using Chuck Interceptor will display a notification showing a summary of ongoing HTTP activity. Tapping on the notification launches the full Chuck UI.

With this, we are able to see HTTP responses in realtime as they are received.

How to add Chuck Interceptor to our app

Start by heading to the Github page and adding the latest version of the dependency to your app-level Gradle file, add the dependency to the dependencies tag and sync your project.

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
    debugImplementation 'com.readystatesoftware.chuck:library:1.1.0'
}

Next, you need to add Chuck Interceptor to your OkHttpClient instance and plug the client into your Retrofit builder, so you do:

OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new ChuckInterceptor(this))
                .build();

Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("https://Peterstev.com")
                .client(client)
                .build();

MyApiInterface myInterface = retrofit.create(MyApiInterface.class);
//initiate request...

Now when you make a request to your endpoint, you can see the requests being logged in the notification tray and you can open up the requests to see whats going on.

Now you obviously do not want your users to see the endpoints you are hitting when the app is in production, hence you add a new line to your dependencies tag.

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
    debugImplementation 'com.readystatesoftware.chuck:library:1.1.0'
    releaseImplementation 'com.readystatesoftware.chuck:library-no-op:1.1.0'
}

With the addition of the ” no-op release implementation variant”, when you build your app using a release build variant, chuck will be isolated and won’t ship alongside your production code.

Additionally, you can also disable showing the Notification in your App by calling showNotification(false) on the interceptor instance.

OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new ChuckInterceptor(context)
                        .showNotification(false))
                .build();

Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("https://Peterstev.com")
                .client(client)
                .build();

MyApiInterface myInterface = retrofit.create(MyApiInterface.class);
//initiate request...

Then you can launch the Chuck UI from with your app by calling

Chuck.getLaunchIntent() 

So there you go with Logging and Intercepting Http requests in Android Applications.

Do like and share this post on Twitter, Facebook and LinkedIn or other social media platforms if you found it helpful.

If you have any issues don’t fail to reach out to me in the comments box and I’ll do well to promptly reply you.

Thanks.

Total Page Visits: 5136 - Today Page Visits: 3
Share

You may also like...

1 Response

  1. Gabe Patrick says:

    Nice Post, I’ll try this out tomorrow.

Leave a Reply

Your email address will not be published. Required fields are marked *