How I can use Gson in Retrofit library?
You don't even need to make a custom deserializer here.
Get rid of UserDeserializer
entirely, it's not needed. Your query is returning a list of movies, so make your callback to an object that actually reads the list of movies:
public class MovieList {
@SerializedName("results")
List<Movie> movieList;
// you can also add page, total_pages, and total_results here if you want
}
Then your GitMovieApi
class would be:
public interface GitMovieApi {
@GET("/3/movie/{movie}")
public void getMovie(@Path("movie") String typeMovie,
@Query("api_key") String keyApi,
Callback<MovieList> response);
}
Your RestAdapter
:
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setConverter(new GsonConverter(new GsonBuilder()).create()))
.setEndpoint("http://api.themoviedb.org")
.build();
GitMovieApi git = restAdapter.create(GitMovieApi.class);
The problem is not that you have written the Deserializer
incorrectly (although, you have, but it's okay because you don't need it, JsonParser
is not how you do it), but the default deserialization behavior should work just fine for you. Use the above code and it will work just fine.
In Retrofit 2 it is even simpler. Your GitMovieApi
class:
interface MoviesApi {
@GET("/3/movie/{movie}")
Call<MovieList> getMovies(@Path("movie") String typeMovie,
@Query("api_key") String keyApi);
}
And than you just need to create a Retrofit object, and make a callback:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(MOVIES_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(MoviesApi.class);
Call<MovieList> mlc = service.getMovies(getArguments().getString(ARG_MOVIE_TYPE), getString(R.string.THE_MOVIE_DB_API_TOKEN));
mlc.enqueue(new Callback<MovieList>() {
@Override
public void onResponse(Call<MovieList> call, Response<MovieList> response) {
movies = response.body().movieList;
}
@Override
public void onFailure(Call<MovieList> call, Throwable t) {}
});