Reputation: 221
I'm getting the following error when trying to parse JSON response with Retrofit:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
This is what the JSON response looks like:
{
"automobi": [{
"Dealers": {
"Dealer": [{
"@attributes": {
"Id": "12",
"Name": "Barons Tokai",
"Region": "Western Cape",
"Status": "active",
"UniqueKey": "96ec67999ed613174efe17f697dc4695"
}
}, {
"@attributes": {
"Id": "13",
"Name": "Barons Pietermaritzburg",
"Region": "KwaZulu-Natal",
"Status": "active",
"UniqueKey": "0f0637a035a9bc98ac67aa30ffc1367e"
}
}, {
"@attributes": {
"Id": "36",
"Name": "Barons Bellville",
"Region": "Western Cape",
"Status": "active",
"UniqueKey": "9ec8f24fdee47f14ce56e9a3fc2689a4"
}
}, {
"@attributes": {
"Id": "37",
"Name": "Barons Bruma",
"Region": "Gauteng",
"Status": "active",
"UniqueKey": "de7ac02f51463d94cb11947c1bf5589c"
}
}, {
"@attributes": {
"Id": "38",
"Name": "Barons Culemborg",
"Region": "Western Cape",
"Status": "active",
"UniqueKey": "de2d98530d1ac95218c2eb5d25d847b2"
}
}, {
"@attributes": {
"Id": "39",
"Name": "Barons N1 City",
"Region": "Western Cape",
"Status": "active",
"UniqueKey": "a055a9a6fa1c9a43cced2335ce6eefcb"
}
}, {
"@attributes": {
"Id": "40",
"Name": "Barons Durban",
"Region": "KwaZulu-Natal",
"Status": "active",
"UniqueKey": "8e49cb3d88b54d144c6728a450b2f491"
}
}, {
"@attributes": {
"Id": "41",
"Name": "Barons Woodmead",
"Region": "Gauteng",
"Status": "active",
"UniqueKey": "097722a842f43f0c59cfac14694b8443"
}
}]
}
}]
}
This is my Dealer class:
public class Dealer {
@SerializedName("Id")
String mId;
@SerializedName("Name")
String mName;
@SerializedName("Region")
String mRegion;
@SerializedName("UniqueKey")
String mUniqueKey;
public String getId() {
return mId;
}
public void setId(String id) {
this.mId = id;
}
public String getName() {
return mName;
}
public void setName(String name) {
this.mName = name;
}
public String getRegion() {
return mRegion;
}
public void setRegion(String region) {
this.mRegion = region;
}
public String getUniqueKey() {
return mUniqueKey;
}
public void setUniqueKey(String uniqueKey) {
this.mUniqueKey = uniqueKey;
}
}
This is the Retrofit call:
@FormUrlEncoded
@POST("get_dealer_list.php")
Call<List<Dealer>> getDealers(@Field("listname") String listName, @Field("outputformat") String outputFormat);
Any ideas for what I'm doing wrong?
Upvotes: 0
Views: 221
Reputation: 3751
For the smallest child
"@attributes": { "Id": "12", "Name": "Barons Tokai", "Region": "Western Cape", "Status": "active", "UniqueKey": "96ec67999ed613174efe17f697dc4695" }
public class DealerObject {
@SerializedName("@attribute")
Dealer attribute;
public getAttribute() {
return attribute;
}
}
For the next one
"Dealer": [ { "@attributes": { "Id": "12", "Name": "Barons Tokai", "Region": "Western Cape", "Status": "active", "UniqueKey": "96ec67999ed613174efe17f697dc4695" } }, { "@attributes": { "Id": "13", "Name": "Barons Pietermaritzburg", "Region": "KwaZulu-Natal", "Status": "active", "UniqueKey": "0f0637a035a9bc98ac67aa30ffc1367e" } }, { "@attributes": { "Id": "36", "Name": "Barons Bellville", "Region": "Western Cape", "Status": "active", "UniqueKey": "9ec8f24fdee47f14ce56e9a3fc2689a4" } }, { "@attributes": { "Id": "37", "Name": "Barons Bruma", "Region": "Gauteng", "Status": "active", "UniqueKey": "de7ac02f51463d94cb11947c1bf5589c" } }, { "@attributes": { "Id": "38", "Name": "Barons Culemborg", "Region": "Western Cape", "Status": "active", "UniqueKey": "de2d98530d1ac95218c2eb5d25d847b2" } }, { "@attributes": { "Id": "39", "Name": "Barons N1 City", "Region": "Western Cape", "Status": "active", "UniqueKey": "a055a9a6fa1c9a43cced2335ce6eefcb" } }, { "@attributes": { "Id": "40", "Name": "Barons Durban", "Region": "KwaZulu-Natal", "Status": "active", "UniqueKey": "8e49cb3d88b54d144c6728a450b2f491" } }, { "@attributes": { "Id": "41", "Name": "Barons Woodmead", "Region": "Gauteng", "Status": "active", "UniqueKey": "097722a842f43f0c59cfac14694b8443" } } ]
public class DealerListObject {
List<DealerObject> Dealer;
public getDealer {
return Dealer;
}
}
The Delers big object
"Dealers": { "Dealer": [ { "@attributes": { "Id": "12", "Name": "Barons Tokai", "Region": "Western Cape", "Status": "active", "UniqueKey": "96ec67999ed613174efe17f697dc4695" } }, { "@attributes": { "Id": "13", "Name": "Barons Pietermaritzburg", "Region": "KwaZulu-Natal", "Status": "active", "UniqueKey": "0f0637a035a9bc98ac67aa30ffc1367e" } }, { "@attributes": { "Id": "36", "Name": "Barons Bellville", "Region": "Western Cape", "Status": "active", "UniqueKey": "9ec8f24fdee47f14ce56e9a3fc2689a4" } }, { "@attributes": { "Id": "37", "Name": "Barons Bruma", "Region": "Gauteng", "Status": "active", "UniqueKey": "de7ac02f51463d94cb11947c1bf5589c" } }, { "@attributes": { "Id": "38", "Name": "Barons Culemborg", "Region": "Western Cape", "Status": "active", "UniqueKey": "de2d98530d1ac95218c2eb5d25d847b2" } }, { "@attributes": { "Id": "39", "Name": "Barons N1 City", "Region": "Western Cape", "Status": "active", "UniqueKey": "a055a9a6fa1c9a43cced2335ce6eefcb" } }, { "@attributes": { "Id": "40", "Name": "Barons Durban", "Region": "KwaZulu-Natal", "Status": "active", "UniqueKey": "8e49cb3d88b54d144c6728a450b2f491" } }, { "@attributes": { "Id": "41", "Name": "Barons Woodmead", "Region": "Gauteng", "Status": "active", "UniqueKey": "097722a842f43f0c59cfac14694b8443" } } ] }
public class BigDealerObject {
List<DealerListObject> Dealers;
public getBigDealerObject {
return Dealers;
}
}
Then for all the response:
public class DealerResponse {
List<BigDealerObject> automobi;
public getAutomobi {
return automobi;
}
}
Finally, call:
Call<DealerResponse> getDealers(@Field("listname") String listName, @Field("outputformat") String outputFormat);
Upvotes: 1
Reputation: 67
Sure. As you can see your JSON response starts with an object ({}) while you are trying to assign that to a list (List). To solve this, you should create a class called something like Response which will have the following fields and classes inside:
public class Response {
public DealersClass Dealers;
}
class DealersClass {
public List<Dealer> Dealer;
}
Where the Dealer class is the class you have constructed. An easy way to avoid this kind of error in the future is to just take a look at your response. When you see { it means that you should match this to a Java class. When you see [ it means that you should match it to a Java List.
Hope it helps.
Upvotes: 0