0

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.

  • Please show the definition of the `Movie` class. And the `Result` class (if you have one). – OneCricketeer Jul 26 '16 at 23:32
  • [Duplicate](http://stackoverflow.com/questions/16654042/gson-expected-begin-array-but-was-begin-object) and [another](http://stackoverflow.com/questions/30210000/retrofit-expected-begin-object-but-was-begin-array?rq=1) – OneCricketeer Jul 26 '16 at 23:33
  • *Get playing movies returns an `Observable>`* - I'm not so sure that is true. It returns `{ page: 1, results: []}`, which is an Object – OneCricketeer Jul 26 '16 at 23:35
  • Yes, exactly. It returns an object, but I am only interested in the inner array of results. And every one of these results should be parsed into a Movie object. How can I achieve it? – Javier Ventajas Hernández Jul 27 '16 at 09:35
  • I would need to see the requested classes from my first comment and the interfaces you defined for retrofit. – OneCricketeer Jul 27 '16 at 11:06
  • I got it to work doing the following: use an online POJO generator from the json that I receive. making the retrofit call return Observable. Use a getter method to grab a reference to the list of movies. I just have a question, is it possible to parse the array of movies without using the pojo? – Javier Ventajas Hernández Jul 27 '16 at 22:02
  • It is possible, but you have to stop using Retrofit and parse the response string yourself. Retrofit really relies on the POJO – OneCricketeer Jul 27 '16 at 22:09
  • Then I guess I'll stick with this solution. I'll create a package and dump all the pojos in there. Not the DRYest approach anyway. – Javier Ventajas Hernández Jul 27 '16 at 22:10
  • As long as your Model objects have no overlap, I don't understand how you have WET code – OneCricketeer Jul 27 '16 at 22:12

0 Answers0