Abhishek Mishra
Abhishek Mishra

Reputation: 53

How to handle Null response in Retrofit?

I am facing this issue in many places because I don't have any idea about handling null in retrofit.

For Example:

 1.  "name": "Deadpool 2 Trailer",
    "full_movie": "DEADPOOL_2_Teaser_Trailer__2018_.mp4",
    "story": "",
    "genre": null,
    "censor_rating": " ",
    "release_date": null,

2.     "name": "Den utrolige historie om den kæmpestore pære",
        "full_movie": "Den_utrolige_historie_om_den_k__mpestore_p__re.mp4",
        "story": "XYZ"
        "genre": [
            "Animation"
        ],
        "censor_rating": " ",
        "release_date": "2018-03-05",
        "content_types_id": "1",

As you can see genre is null in first case and in second case it is array type. As my pojo class is of course made on the 2nd type. So when I am getting 1st type response from the server, my application gets stops totally. Debug stuck at one place and nothing happens. Though Application is not crashing. How to handle this ?

Upvotes: 1

Views: 574

Answers (2)

user4571931
user4571931

Reputation:

when getting response that store in into your pojo class. and when set data that time check like

     if (!TextUtils.isEmpty(pojoclassObject.getStory())){

         }

Upvotes: 1

AbhayBohra
AbhayBohra

Reputation: 2117

You can check if key is null by using this code

if(jsonObject.isNull("genre"))
{
// handle null
}

Upvotes: 1

Related Questions