'Android (Java) RetroFit 2 | TMDB Movie API | Class name unfortunately determines if API call is successful
*Using RetroFit 2 in an Android project (Java), I am calling the famous TMDB API server for movies, genres and trailers. For these entities, I made Java objects: Trailer, Genre, movie. As you can see, the movie class is without a capital M. It appears that the movie object does not get instantiated if the class name is set to Movie. The API response is successful however. Whenever I change the class name back to movie, the API call instantiation works again.
Question: how to make Retrofit instantiate the movie without considering what the actual Java class name is?
Class definition that makes the Retrofit call work:
public class movie implements Serializable {
// API attributes
@SerializedName("id")
@Expose
private int id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("release_date")
@Expose
private String releaseDate;
@SerializedName("poster_path")
@Expose
private String imageURL;
@SerializedName("original_language")
@Expose
private String language;
@SerializedName("overview")
@Expose
private String description;
@SerializedName("vote_average")
private float avgRating;
Class definition that makes the Retrofit call not work (class name changed), even though the actually networking is successful:
public class Movie implements Serializable {
// API attributes
@SerializedName("id")
@Expose
private int id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("release_date")
@Expose
private String releaseDate;
@SerializedName("poster_path")
@Expose
private String imageURL;
@SerializedName("original_language")
@Expose
private String language;
@SerializedName("overview")
@Expose
private String description;
@SerializedName("vote_average")
private float avgRating;
Retrofit call (more details are not relevant, since there is always a successful response
public void retrieveMovies() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
TMDBApiService service = retrofit.create(TMDBApiService.class);
Call<NowPlayingMoviesResponse> call = service.retrieveMovies(API_KEY, language, 1);
call.enqueue(new Callback<NowPlayingMoviesResponse>() {
@Override
public void onResponse(@NonNull Call<NowPlayingMoviesResponse> call,
@NonNull Response<NowPlayingMoviesResponse> response) {
Class that packages movies
public class NowPlayingMoviesResponse {
@SerializedName("results")
private List<movie> results;
public NowPlayingMoviesResponse(List<movie> results) {
this.results = results;
}
public void setResults(List<movie> results) {
this.results = results;
}
public List<movie> getMoviesResult() {
return results;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
