Debug HTTP traffic in Android apps

0
53

Problem

Debugging has always been an essential part of software development process in order to build a high-quality software. At times debugging can be tedious if you don’t have the right tools available or the tools are there but are difficult to use. Recently, I also had faced this issue while working on an Android app where I wanted to observe the HTTP calls app is making but couldn’t find a single way to do this.

Solution

To be able to see and debug the HTTP calls by the app to the web server, I then created a library called Android Snooper. Android Snooper is an HTTP inspector which observes all the HTTP calls made by the app and opens up an Activity to see the detailed history of HTTP calls on onShake(When user shakes his device) event.

                                               

Android Snooper leverages the interceptor mechanism provided by HTTP client libraries. All you need to do is initialize Android Snooper using AndroidSnooper.init(this); statement in your Application class and set an instance of SnooperInterceptor to the list of your network interceptors. As of now, we are providing the interceptors for the below libraries.

Didn’t get your HTTP client’s name in the list? No worries, You can still write your own implementation by using Android Snooper's core module and let Android Snooper know about the request being made. Below is given a dummy implementation.

AndroidSnooper androidSnooper = AndroidSnooper.getInstance();
HttpCall httpCall = new HttpCall.Builder()
 .withUrl(httpRequest.getUrl())
 .withPayload(httpRequest.getRequestPayload())
 .withMethod(httpRequest.getMethod())
 .withResponseBody(httpResponse.getResponseBody())
 .withStatusCode(httpResponse.getRawStatusCode())
 .withStatusText(httpResponse.getStatusCode())
 .withRequestHeaders(httpRequest.getHeaders())
 .withResponseHeaders(httpResponse.getHeaders())
 .build();
androidSnooper.record(httpCall)

The above implementation ought to be part of your custom interceptor where you will have access to the required Request and Response object to jot down the required data for Android Snooper to work properly.

Usage

compile ('com.github.jainsahab:Snooper:1.0.2@aar'){ transitive = true
}
compile ('com.github.jainsahab:Snooper-Spring:1.0.2@aar'){ transitive = true
}
compile ('com.github.jainsahab:Snooper-Okhttp:1.0.2@aar'){ transitive = true
}

Warning: As Android Snooper records each and every HTTP call goes through it. Please be cautious as it will record the sensitive information such as Auth Tokens, Headers etc. The only purpose of this library is Debugging. Hence, Debug or QA builds are the only justifiable candidate for this library.

Conclusion

As you can see, Android Snooper records all the HTTP calls and show them in a detailed manner. This tool can really help the DEVs as well as QAs to test the behavior of Android app and backend server it is talking to.

Now no need to configure a proxy to analyze HTTP calls, All you need to do is Shake it off.

LEAVE A REPLY