1

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
        }
Erum
  • 790
  • 3
  • 14
  • 36
  • Add json response here. – Pankaj Kumar Sep 14 '15 at 10:49
  • add the POJO class too – Bhargav Sep 14 '15 at 10:50
  • @Bhargav pls see now i added response and class both – Erum Sep 14 '15 at 11:01
  • @PankajKumar pls check i added both response and class – Erum Sep 14 '15 at 11:01
  • also attach the response of the server when it sends false in user_obj, because the thing while trying to parse user_obj gson is expecting an object not a boolean, and that seems to be the error you are getting – Bhargav Sep 14 '15 at 11:05
  • @Bhargav this is not the issue i just eliminate getters and setter for posting here so that any one can understand code easily the big issue is not of getters and setters i have already made in my class – Erum Sep 14 '15 at 11:06
  • alright i have deleted that comment – Bhargav Sep 14 '15 at 11:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89579/discussion-between-erum-and-bhargav). – Erum Sep 14 '15 at 11:07
  • @Bhargav it cannot be correct from server side how will i handle from android side ? – Erum Sep 14 '15 at 11:44
  • catch this specific exception query the exception message to see if it matches the message you expect then assign null to bcast_obj – Bhargav Sep 14 '15 at 11:48
  • @Bhargav but i have to change type of bcast_obj like if it matches the exception then i want to put false instead of object null ? – Erum Sep 14 '15 at 11:53
  • where will i catch exception query ? – Erum Sep 14 '15 at 11:53

3 Answers3

1

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

Community
  • 1
  • 1
ligi
  • 39,001
  • 44
  • 144
  • 244
1

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.

Bhargav
  • 8,118
  • 6
  • 40
  • 63
  • +1 @Erum If you want to specify that there is no `bcast_obj` in a particular response, use `null` instead of `false` – sotrh Apr 13 '17 at 19:06
0

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);
Hasan El-Hefnawy
  • 1,249
  • 1
  • 14
  • 20