im从服务器得到响应,并且在内部响应中,有一个对象,并且对象包含另一个对象数组,并且一个单独的对象包含对象(此对象有时会向我返回对象,而服务器有时会向我发送假消息),因此如果出现错误,我会得到错误 RetrofitError:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第9774列为BOOLEAN
我该如何解决这个问题?
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 ,请查看某些管道服务器中的“ user_obj”和bcast_obj“在bcast_obj”中向我发送了false,而在某些情况下它没有向我发送bcast_obj“而仅向我发送了user_obj,所以如何处理这两部分。 ..主要问题是服务器在user_obj中向我发送false
HERE是在对象中接收的“ false”
{
"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
}
您可能想使用一个自定义转换器来检测它是布尔对象还是json对象。您可能想看看这个答案https://stackoverflow.com/a/28576252/322642
如果服务器希望bcast_obj为对象,则必须保持一致,然后不应将其作为适当的json对象发送(即以大括号开头和结尾),而不应将其作为布尔值发送,因为它将完全改变对象的签名。
这可能不是最佳解决方案,但是您可以将JsonElement的BcastObj替换为响应bcast_obj的数据类型,该响应以对象或布尔值的形式返回。 JsonElement类是JsonObject和JsonPrimitive的父类。这是一个例子:
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
您还可以考虑将bcast_obj(在将其数据类型设置为JsonElement而不是BcastObj之后,将其转换为字符串,并检查其是否包含特定数据或仅包含布尔值。转换可以如下进行:
String bcast_objString = new Gson().toJson(bcast_obj);