Here is my sample JSON when a user authenticates successfully
{
"status": "success",
"message": "",
"data": {
"_id": {
"$id": "..."
},
"email": "...",
"name": "...",
"mobile": "...",
"mobile_status": "...",
"email_status": "..."
}
}
And when user credentials are wrong
{
"status": "error",
"message": {
"msg": [
"Login detail incorrect"
]
},
"data": ""
}
Notice the change in datatypes of message
& data
. Using retrofit fit with this I received famously error Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
To resolve this I followed this example to resolve the issue. Which did resolve the OBJECT/ARRAY issue.
I have 3 Models LoginResponse
,MessageResponse
,DataResponse
and this is how I am trying to get access to MessageResponse
& DataResponse
I'm not sure if i can even use 2
Gson messageDeserializer = new GsonBuilder().setLenient().registerTypeAdapter(LoginResponse.class, new MessageDeserializer()).create();
Gson dataDeserializer = new GsonBuilder().setLenient().registerTypeAdapter(LoginResponse.class, new DataDeserializer()).create();
builder.addConverterFactory(GsonConverterFactory.create(messageDeserializer));
builder.addConverterFactory(GsonConverterFactory.create(dataDeserializer));
I don't think these custom JsonDeserializer
are working the way its supposed to. I'm unable to cast returned response from these JsonDeserializer. The only way I can access them as string only which isn't even a proper JSON formatted string.
Also switching back order of .addConverterFactory sometimes causes a crash when i access LoginResponse
model