1

i Have 2 responses in my api response one for success if the data is correct and one for incorrect data i can't handle it with my pojo class and this is my class

public class AirCraftSearchResponse {

@SerializedName("Data")
@Expose
private Data data;
@SerializedName("Message")
@Expose
private Object message;
public Data getData() {
    return data;
}

public void setData(Data data) {
    this.data = data;
}

public Object getMessage() {
    return message;
}

public void setMessage(Object message) {
    this.message = message;
}

and this is the two responses of faill

{
"Success": false,
"Message": "Could not convert string to DateTime: 2019-12-08T0000:00. Path 'Legs[0].DateTime', line 1, position 1185.",
"Data": [],
"total": 0
}

and success response is

{
"Success": true,
"Message": "",
"Data": {},
"total": 0
}

when i make response and i get success there is no problem happen because my pojo class have same object for response and when i get fail it make exception and i can't handle or get it's message because it goes to onFailure method when i print it's message i get this line

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 143 path $.Data

1 Answers1

0

The problem itself says

Expected BEGIN_OBJECT but was BEGIN_ARRAY

The issue with your value type of key "data". In success, you are sending Object i.e. {} and in case of failure, you are sending an array i.e. []. Try to send object {} in failure and your problem would be solved.

Right fomat below for failure

{
 "Success": false,
 "Message": "Could not convert string to DateTime: 2019-12-08T0000:00. 
 Path 'Legs[0].DateTime', line 1, position 1185.",
 "Data": {},
 "total": 0
}
UMESH0492
  • 1,701
  • 18
  • 31
  • It's a format problem from backend or wherever you are creating that JSON. – UMESH0492 Feb 04 '18 at 18:40
  • it's not request iam don't sending this data its back to me from api response and its back with two ways i want to handle it and i can't edit in api structure it's not my api – Yehia Ahmad Feb 04 '18 at 19:06
  • i know it's wrong format from back end and i want to handle it by creating 2 model or something like that you understand me ? – Yehia Ahmad Feb 04 '18 at 19:07
  • In that case, you need to serialize the data after receiving the JSON. Or Write your own Gson deserializer for your Data element. This would check whether the data element is an object or an array. – UMESH0492 Feb 04 '18 at 19:24
  • i don't know how to serialize the data can you explain to me ? how to maka it or how to make gson deserializer – Yehia Ahmad Feb 04 '18 at 20:35