Retrofit 2 — Handling timeout, errorbody

Configure timeouts settings, Check a node exists or not, Get errorbody response.

SHISHIR
PROGRAMMING LITE
3 min readOct 16, 2018

--

Image result for Retrofit 2

Retrofit, most popular networking library in the recent times. While working with retrofit i faced some issues/cases needed to be handled. Here i am sharing those issues. This may be helpful for you. The issues are:

  1. Configure Timeout settings.
  2. Check a node exists or not in response.
  3. Get errorbody response .

Configure Timeout Settings

We can set timeouts settings on the underlying HTTP client. If we don’t specify a client, Retrofit will create one with default connect and read timeouts. By default, Retrofit uses the following timeouts:

  • Connection timeout: 10 seconds
  • Read timeout: 10 seconds
  • Write timeout: 10 seconds

To customize the timeouts settings you need to configure OkHttp, Retrofit’s network layer. See the code below.

OkHttpClient okHttpClient = new OkHttpClient.Builder()  
.connectTimeout(1, TimeUnit.MINUTES)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
.build();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http:*******")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create());
  • Here, the connection timeout to 1 minute, the read timeout to 30 seconds, and the write timeout to 15 seconds.
  • Of course there is no need to change all three at the same time. You can just modify whatever you want to modify.

Check a Node exist or not in response

  • Convert the response to the JsonObject and use has() method to figure out if the response has that particular field or not. For example see the code below:
String jsonResponse = new Gson().toJson(response.body());try{
JSONObject jsonObject = new JSONObject(jsonResponse);
if(jsonObject.has("<keyName>")){
String value = jsonObject.getString("<keyName>");
}else{
// the key is not present
}
}catch (Exception e){

}
  • You may use optString("<keyName>") instead of getString("<keyName>") to get the value and in this case there is no need to check with has() method weather the node is present or not. Because optString returns the empty string ("") if the key you specify doesn't exist.
  String jsonResponse = new Gson().toJson(response.body());  try{
JSONObject jsonObject = new JSONObject(jsonResponse);
String value = jsonObject.optString("<keyName>");
}catch (Exception e){

}

If you want to avoid NullPointerException you better make use of optString()

Get ErrorBody Response in onError(Throwable e)

When we make any bad request (HTTP 400) Retrofit doesn’t convert the response. In this case we can access the raw response with response.errorBody.string(). For better understanding see the code below.

Thanks for reading this article. Be sure to give claps if you find this article helpful.

Are you getting bored to check Internet Connectivity again and again before each API Call ???. Then you can see my another article Retrofit 2 — Best way for Handling Network Error.

Do you know it is very easy to decompile an Android App and get the API you used in your project???? To secure your API Keys you can see this article Securing API Keys in Android App

Always try to use PROGUARD in your Android app to keep your codes secured. If you have no idea on Proguard then you can see this article Secure Your Codes by Enabling PROGUARD in you Anroid App.

Happy Coding :)

--

--

SHISHIR
PROGRAMMING LITE

{ 'designation' : 'Lead Software Engineer' , 'hobby' : [ 'Music', 'Photography', 'Travelling' ] ,’email’: ‘shishirthedev@gmail.com’ }