Retrofit debugging with Okhttp logging interceptor

Sometimes while working with Retrofit and parsing JSON data you may get some weird errors similar to this,
Error :JsonReader.setLenient(true)to accpect malformed JSON at line 1 Column 1 path $
The problem is the JSON response is not valid so that the Gson converter cannot serialize it. You have two options to find the cause for this issue. One is to use Postman to make the request and check the response or the other is to attach Okhttp logging interceptor to Retrofit and check the logs.
Open your app’s build.gradle and add the following dependency along with other Retrofit dependencies. Then sync the project.
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
Where 3.3.1 is the version number which is same as Okhttp version. You can find the latest version using Gradle please or from Okhttp project page.
Now you just need to add just 4 lines of code to your existing Retrofit project. First create a new OkhttpClient.Builder object,
OkHttpClient.Builder client = new OkHttpClient.Builder();
Next create a new HttpLoggingInterceptor and set logging level,
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
Where logging level is responsible for the information you want to print in your log.
BODY prints everything. HEADERS prints the headers of the Response excluding Response body. BASIC just prints the Request method such as GET, POST along with url, http version, Response code such as 200, 404.
Then add the interceptor to Okhttp client,
client.addInterceptor(loggingInterceptor);
Finally add the Okhttp client to Retrofit using client() method.
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://api.learn2crack.com")
        .client(client.build())
        .addConverterFactory(GsonConverterFactory.create())
        .build();
where client is the OkhttpClient.Builder object. Its done.
For example a GET request to the url http://api.learn2crack.com/android/jsonandroid/ prints the following,
D/OkHttp: --> GET http://api.learn2crack.com/android/jsonandroid/ http/1.1
D/OkHttp: --> END GET
D/OkHttp: <-- 200 OK http://api.learn2crack.com/android/jsonandroid/ (98ms)
D/OkHttp: Server: nginx/1.6.2 (Ubuntu)
D/OkHttp: Date: Sun, 05 Jun 2016 02:25:31 GMT
D/OkHttp: Content-Type: text/html; charset=UTF-8
D/OkHttp: Vary: Accept-Encoding
D/OkHttp: Transfer-Encoding: chunked
D/OkHttp: Connection: keep-alive
D/OkHttp: { "android": [ { "ver": "1.5", "name": "Cupcake", "api": "API level 3" }, { "ver": "1.6", "name": "Donut", "api": "API level 4" }, { "ver": "2.0 - 2.1", "name": "Eclair", "api": "API level 5 - 7" }, { "ver": "2.2", "name": "Froyo", "api": "API level 8" }, { "ver": "2.3", "name": "Gingerbread", "api": "API level 9 - 10" }, { "ver": "3.0 - 3.2", "name": "Honeycomb", "api": "API level 11 - 13" }, { "ver": "4.0", "name": "Ice Cream Sandwich", "api": "API level 14 - 15" }, { "ver": "4.1 - 4.3", "name": "JellyBean", "api": "API level 16 - 18" }, { "ver": "4.4", "name": "KitKat", "api": "API level 19" }, { "ver": "5.0 - 5.1", "name": "Lollipop", "api": "API level 21 - 22" }, { "ver": "6.0", "name": "Marshmallow", "api": "API level 23" } ] }
D/OkHttp: <-- END HTTP (744-byte body)
With these logs you can check whether you have set your headers properly, check expected JSON response.
You can get the sample project from Github,
Happy Coding 🙂

Comments