I need to get a list of reviews using Retrofit from the following api query:
http://api.themoviedb.org/3/movie/{id}/reviews?api_key=#######
The JSON returned is:
{"id":83542,"page":1,"results":[{"id":"51910979760ee320eb020fc2","author":"Andres Gomez","content":"Interesting film with an exceptional cast, fantastic performances and characterizations. The story, though, is a bit difficult to follow and, in the end, seems to not have a real point.","url":"https://www.themoviedb.org/review/51910979760ee320eb020fc2"},{"id":"520a8d10760ee32c8718e6c2","author":"Travis Bell","content":"Cloud Atlas was a very well made movie but unlike most of the \"simultaneous stories that all come together at the end\" type of movies, this one just didn't. I'm still unclear as to the point of it all.\r\n\r\nAnother issue I had was a general feeling of goofiness. Sure, the Cavendish story was pure comedy but the rest of the stories just didn't feel serious enough to me.\r\n\r\nIt carried my attention for the 172 minutes well enough and it was entertaining. I just expected more of a pay off at the end.\r\n\r\nAll in all, it's definitely worth seeing but I still haven't made up my mind if I truly liked it or not. What did you think?","url":"https://www.themoviedb.org/review/520a8d10760ee32c8718e6c2"}],"total_pages":1,"total_results":2}
My goal is to to access the "content" attribute and then put in a TextView.
Where I am having trouble is, for some reason I cannot figure out how to return a list of result objects from the above JSON and then access the content attribute using getContent().
I think there may be a problem with the way I am using my model object/s? Should they be two seperate objects? I'm not sure. When I try to print out the objects I get empty responses...
Any help would be great.
Retrofit interface code is:
public class MovieService {
public interface MovieDbApi {
@GET("/3/movie/{id}?&append_to_response=reviews")
Call<MovieReview> getReviews(
@Path("id") int id,
@Query("api_key") String apiKey);
}
}
Model object code is:
POJO 1
public class MovieReview {
@SerializedName("id")
@Expose
private int id;
@SerializedName("page")
@Expose
private int page;
@SerializedName("results")
@Expose
private List<MovieReviewDetail> results = new ArrayList<MovieReviewDetail>();
@SerializedName("total_pages")
@Expose
private int totalPages;
@SerializedName("total_results")
@Expose
private int totalResults;
/**
*
* @return
* The id
*/
public int getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(int id) {
this.id = id;
}
/**
*
* @return
* The page
*/
public int getPage() {
return page;
}
/**
*
* @param page
* The page
*/
public void setPage(int page) {
this.page = page;
}
/**
*
* @return
* The results
*/
public List<MovieReviewDetail> getResults() {
return results;
}
/**
*
* @param results
* The results
*/
public void setResults(List<MovieReviewDetail> results) {
this.results = results;
}
/**
*
* @return
* The totalPages
*/
public int getTotalPages() {
return totalPages;
}
/**
*
* @param totalPages
* The total_pages
*/
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
/**
*
* @return
* The totalResults
*/
public int getTotalResults() {
return totalResults;
}
/**
*
* @param totalResults
* The total_results
*/
public void setTotalResults(int totalResults) {
this.totalResults = totalResults;
}
}
POJO 2
public class MovieReviewDetail {
@SerializedName("id")
@Expose
private int id;
@SerializedName("author")
@Expose
private String author;
@SerializedName("content")
@Expose
private String content;
@SerializedName("url")
@Expose
private String url;
/**
*
* @return
* The id
*/
public int getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(int id) {
this.id = id;
}
/**
*
* @return
* The author
*/
public String getAuthor() {
return author;
}
/**
*
* @param author
* The author
*/
public void setAuthor(String author) {
this.author = author;
}
/**
*
* @return
* The content
*/
public String getContent() {
return content;
}
/**
*
* @param content
* The content
*/
public void setContent(String content) {
this.content = content;
}
/**
*
* @return
* The url
*/
public String getUrl() {
return url;
}
/**
*
* @param url
* The url
*/
public void setUrl(String url) {
this.url = url;
}
}
Fragment code:
private static final String API_KEY = "#####";
private static final String API_BASE_URL_MOVIES = "http://api.themoviedb.org/";
private Call<MovieReview> call;
private MovieReview movieReview;
private List <MovieReviewDetail> movieReviewDetails;
public void getMovieReview(int id){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL_MOVIES)
.addConverterFactory(GsonConverterFactory.create())
.build();
MovieService.MovieDbApi movieDbApi = retrofit.create(MovieService.MovieDbApi.class);
call = movieDbApi.getReviews(id, API_KEY);
call.enqueue(new Callback<MovieReview>() {
@Override
public void onResponse(Response<MovieReview> response) {
if (!response.isSuccess()) {
Log.d(LOG_TAG, "Error");
}
movieReview = response.body();
movieReviewDetails = movieReview.getResults();
//Print out results
for (int i = 0; i < movieReviewDetails.size(); i++) {
System.out.println(movieReviewDetails.get(i).getContent());
}
}
@Override
public void onFailure(Throwable t) {
Log.e("Throwable: ", t.getMessage());
}
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
getMovieReview(83542);
}