Reputation: 29
Hello I'm starting to learn building apps for Android. I'm using Retrofit to get a json response. This is the structure:
{"Results": [
{
"Id": 1,
"Feed": 1,
"Title": "title",
"Summary": "Text",
"PublishDate": "2015-09-12T21:45:16",
"Image": "imageurl",
"Url": "websiteurl",
"Related": [
"relatedurl",
"relatedurl"
],
"Categories": [
{
"Id": 61,
"Name": "Sport"
},{
"Id": 63,
"Name": "Voetbal"
}
],
"IsLiked": false
}
],
"NextId": 4285 }
These are my models:
public class Result {
private String NextId;
private Results[] Results;
}
public class Results {
private String Url;
private String Feed;
private String Image;
private String Id;
private List<String> Related;
private List<Categories> Categories;
private String PublishDate;
private String Title;
private String IsLiked;
private String Summary;
}
public class Categories {
private String Name;
private String Id;
}
And my interface looks like this:
public interface WebService {
@GET("articles")
Call<List<Result>> Articles();
}
I got a fatal error during runtime:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
Can anyone tell me what I'm doing wrong?
Upvotes: 0
Views: 874
Reputation: 29
Add DTO Plugin in Android studio. Data Transfer Object(DTO) generator that generates Java classes from the given feed.
Here is your correct model class
{
public static class Results {
@SerializedName("Id")
public int id;
@SerializedName("Feed")
public int feed;
@SerializedName("Title")
public String title;
@SerializedName("Summary")
public String summary;
@SerializedName("PublishDate")
public String publishdate;
@SerializedName("Image")
public String image;
@SerializedName("Url")
public String url;
@SerializedName("Related")
public List<Related> related;
@SerializedName("Categories")
public List<Categories> categories;
@SerializedName("IsLiked")
public boolean isliked;
}
public static class Categories {
@SerializedName("Id")
public int id;
@SerializedName("Name")
public String name;
}
}
Upvotes: 1
Reputation: 1
Do the following changes
Your interface should be change to
public interface WebService {
@GET("articles")
Call<Result> Articles();
}
in your model class
public class Result {
private String NextId;
private List<Results> Results;
}
Upvotes: 0
Reputation: 469
id, feed and NextId are not string, you are fetching data with a wrong structure . And try with
Call<Result> Articles()
Upvotes: 0
Reputation: 157487
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
The endpoint returns a JSONObject
but you are telling retrofit that is returning a JSONArray
.
Change
Call<List<Result>> Articles();
with
Call<Result> Articles();
also NextId
and Id
in the JSON
you posted are not String
Upvotes: 3