Reputation: 55
I'm working with a login api the sends a complex response when login is successful. I'm having problems implementing the retrieved array in the POJO class created for the response. The array contains two nested objects.
I tried using an arraylist to retrieve it but I'm getting an error
public class User{
String id;
String name;
String email;
String email_verified_at;
@SerializedName("user_agents")
private ArrayList<UserAgents> userAgents ;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail_verified_at() {
return email_verified_at;
}
public void setEmail_verified_at(String email_verified_at) {
this.email_verified_at = email_verified_at;
}
UserAgents.java
public class UserAgents{
public UserAgents(){
device = "";
token_id = "";
}
@SerializedName("device")
String device;
@SerializedName("token_id")
String token_id;
public String getDevice() {
return device;
}
public void setDevice(String device) {
this.device = device;
}
public String getToken_id() {
return token_id;
}
public void setToken_id(String token_id) {
this.token_id = token_id;
}
}
The expected JSON format is:
"user_agents": "
[
{"device":"Mozilla/5.0 (Macintosh; Intel Mac OS X x.y;
rv:42.0) Gecko\/20100101 Firefox/42.0",
"token_id":"ec4eccb011beb3c41f11e83f670ae635117770ac7bc9fb2ac3fbdac3645c16e63e64038d2ad0aa3c"
},
{"device":"Mozilla\/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0",
"token_id":"8e2bfef45fda2387ede9aa06d71247873db6390f3bf1e5c704cf51e76e3ecbd25cacdfb276464404"
}
]"
This is the error I'm getting:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 145 path $.success.user.user_agents
Upvotes: 0
Views: 86
Reputation: 329
*THIS WILL WORK *
JSONObject JO = new JSONObject(jsonresponse);
JSONArray JA = (JSONArray) JO.getString("user_agents");
for (int i = 0; i < JA.length(); i++)
{
JSONObject jo = (JSONObject)JA.get(i);
UserAgents UA = new UserAgents();
UA.device = jo.getString("device");
UA.token_id =jo.getString("token_id");
userAgents.add(UA);
}
Upvotes: 0
Reputation: 195
Because of the user_agents
contains ""
So there is your user_agents
is String
You need to remove ""
from "user_agents": [ ]
{
"user_agents": [
{
"device": "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0",
"token_id": "ec4eccb011beb3c41f11e83f670ae635117770ac7bc9fb2ac3fbdac3645c16e63e64038d2ad0aa3c"
},
{
"device": "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0",
"token_id": "8e2bfef45fda2387ede9aa06d71247873db6390f3bf1e5c704cf51e76e3ecbd25cacdfb276464404"
}
]
}
Upvotes: 1
Reputation: 186
your response handling is not proper, as per error, web-service response is type of string but you set model class which is type of array,
or share web-service response for better solution.
removing "user_agents" keyword from response.
your web-service response must be this,
[ {"device":"Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0",
"token_id":"ec4eccb011beb3c41f11e83f670ae635117770ac7bc9fb2ac3fbdac3645c16e63e64038d2ad0aa3c" }, {"device":"Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0", "token_id":"8e2bfef45fda2387ede9aa06d71247873db6390f3bf1e5c704cf51e76e3ecbd25cacdfb276464404" } ]
Upvotes: 0