I make calls to an api that returns the following structure:
{
page: 1,
results: [
{
poster_path: "/6FxOPJ9Ysilpq0IgkrMJ7PubFhq.jpg",
adult: false,
overview: "Tarzan, having acclimated to life in London, is called back to his former home in the jungle to investigate the activities at a mining encampment.",
release_date: "2016-06-29",
genre_ids: [
28,
12
],
id: 258489,
original_title: "The Legend of Tarzan",
original_language: "en",
title: "The Legend of Tarzan",
backdrop_path: "/75GFqrnHMKqkcNZ2wWefWXfqtMV.jpg",
popularity: 27.931248,
vote_count: 655,
video: false,
vote_average: 4.6
},
... MORE MOVIES ...
}
The part that I am interested in is JUST the array of movie objects. My movie class has the following method:
public static TypeAdapter<Movie> typeAdapter(Gson gson) {
return new AutoValue_Movie.GsonTypeAdapter(gson);
}
And this is how I build the Gson object that I pass to my retrofit instance:
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new AutoValueGsonTypeAdapterFactory())
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.create();
I use the following method in my DataManager to make the api call:
public Observable<Movie> syncMovies(int page) {
return mMovieService.getPlayingMovies(page)
.concatMap(new Func1<List<Movie>, Observable<Movie>>() {
@Override
public Observable<Movie> call(List<Movie> movies) {
return mDatabaseHelper.setMovies(movies);
}
});
}
Get playing movies returns an Observable< List< Movie > >. The problem is that I get an error message saying: Expected BEGIN_ARRAY but was BEGIN_OBJECT.
I think that the problem is that my JSON is really an object. How can I just extract the array and parse it into a List< Movie >?
Sorry for the ignorance but I am completely new to Gson.
>`* - I'm not so sure that is true. It returns `{ page: 1, results: []}`, which is an Object
– OneCricketeer Jul 26 '16 at 23:35