Logging Android OKHttp request and response
Yeah! Do you think checking the logs for requests and responses of JSON is easy? It’s like a nightmare to Android Developers unless and until you ping the request and response (JSON) in the Logcat or extend Logging interceptors (Not a bad idea).
Traditional Methods:
- ADB Logcat
It really works, but the device needs to be connected either through wifi or USB
Should Kill, Restart and Start ADB if at all needed.
Boring UI to check in the black old Terminal window.
2. Cat Log — Ooops! Most of the time, your logs will not be displayed ;-)
3. Stetho Integration — Good idea! Doesn’t seem to be capable enough to check on the fly. But how will you check your headers, form data, etc.?
4. Ngrok — Good for backend engineers. But really bad idea to change your base url’s in your code to bypass your requests.
How to check your network request and response like a Boss?
Chuck:
Why you wanna waste your time by logging the request and response in your code and later removing those during your release builds?
Just chuck it! and check it!
Integrations: (So simple)
Add the dependency in your build.gradle
file. Add it alongside the no-op
variant to isolate Chuck from release builds as follows:
dependencies {
debugCompile 'com.readystatesoftware.chuck:library:1.0.4'
releaseCompile 'com.readystatesoftware.chuck:library-no-op:1.0.4'
}
In your application code, create an instance of ChuckInterceptor
(you'll need to provide it with a Context
, because Android) and add it as an interceptor when building your OkHttp client:
OkHttpClient client
= new OkHttpClient.Builder().addInterceptor(new ChuckInterceptor(context)).build();
That’s it! Chuck will now record all HTTP interactions made by your OkHttp client. You can optionally disable the notification by calling showNotification(false)
on the interceptor instance, and launch the Chuck UI directly within your app with the intent from Chuck.getLaunchIntent()
.
Now it’s your turn to throw the ball on backend developer’s face. Your testers will be happy to test your application :-)
**Credits: (**I didn’t do much)
Github Repository: https://github.com/jgilfelt/chuck
Subscribe to my newsletter
Read articles from Chandrasekar Kuppusamy directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by