1

I am using Retrofit 2.1.0 library, and use call.enqueue method to do async API calls.

The CallBack class returns onResponse(Call call, Response response) and onFailure (Call call, Throwable t).

My question is that, is it safe to assume that the Response ( not response.body) object in the onResponse method will always be NonNull?

If it can be null, what can be the possible scenarios?

I looked online and on Retrofit docs, but no clear answers.

Akshay
  • 806
  • 11
  • 26

1 Answers1

4

It shouldn't be null.

You can see in Retrofit source code that you only get a Response.success or a Reponse.error returned.

You can check if the HTTP request was a success using Response.isSuccessful().

If the Response was possible to be null, then that method would throw a NullPointerException.

If there was a scenario for a null Response, or some exception thrown when parsing the response, then onFailure would be entered.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245