Erum
Erum

Reputation: 790

how to handle type boolean and object in retrofit response?

i m getting response from server and inside response there is object and object contains another arrayofobjects and an individual object contains object (this object is sometimes returning me object and sometimes server is sending me false) so in case of false i m getting error RetrofitError:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BOOLEAN at line 1 column 9774

how can i resolve this issue ?

public class NotificationResponse {

    private UserNotificaions User_Notificaions;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
}
public class UserNotificaions {
 private List<Record> records = new ArrayList<Record>();
    private Pager pager;
    private Integer nextPage;
    private Integer prePage;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
}
public class Record{
private String name;
private String title;
private String profile_image;
private String id;
private String uid;
private String notification_type_id;

private String bid;
private String is_read;
private String total_notifications;
private BcastObj bcast_obj;
    private User user_obj;
}
public class BcastObj {
    private String bid;
    private String name;
    private String message;
    private String tags;
}
public class User
{

    private String total;

    private String broadcast_comment_id;

    private String bid;
}

SERVER response please take a look into "user_obj" and bcast_obj" in some conduitions server is sending me false in bcast_obj" while in some its not sending me bcast_obj" and only sending me user_obj so how to handle these two parts .... the major issue is once server is sending me false in user_obj

HERE is the " false " reciving in an object

 {
            "name": null,
            "title": "NewBroadcastcreated",
            "message": "abcihascreatednewbroadcast.",
            "full_name": "abci",
            "profile_thumb": "",
            "profile_image": "",
            "id": "C24-F5C8-DD24-4FF6-44978782082F",
            "uid": "4E8A0-F3DD-C41D-4105-6443222CAB3C",
            "notification_type_id": "2",
            "is_created": "1420",
            "sender_id": "F492B0-FB8C-8CD8-4C51-D77BEC987A78",
            "type": "general",
            "bid": "E1D-4426-B095-5F55-18A120F8C9D6",
            "is_read": "1",
            "is_notify_popup": "0",
            "total_notifications": "937",
            "bcast_obj": false
        }

Upvotes: 1

Views: 5687

Answers (3)

Hasan El-Hefnawy
Hasan El-Hefnawy

Reputation: 1569

It may not be the best solution, but you may replace BcastObj with JsonElement as a data type for the response bcast_obj that returns either as an Object or as a boolean. JsonElement class is the parent class for both JsonObject and JsonPrimitive. Here is an example:

if (response.body().getBcastObj().getAsJsonObject().get("name").getAsString().equals("stringValue"))
    // write code for the first case when bcast_obj returns as an Object
else if (!response.body().getBcastObj().getAsBoolean())
    // write code for the second case when bcast_obj returns as a boolean false

You may also consider converting bcast_obj (after making its data type as JsonElement instead of BcastObj) to string and check if it contains specific data or just contains a boolean value. The conversion can be made as following:

String bcast_objString = new Gson().toJson(bcast_obj);

Upvotes: 0

Bhargav
Bhargav

Reputation: 8277

server has to be consistent if it wants bcast_obj to be object then it should be sent as a proper json object (i.e starting and ending with braces) shouldn't be sent as boolean as it will completely change the signature of your object.

Upvotes: 1

ligi
ligi

Reputation: 39539

You might want to use a custom converter that detects if it is a boolean or an json-object. You might want to have a look at this answer https://stackoverflow.com/a/28576252/322642

Upvotes: 1

Related Questions