implement displayMoviesByGenre method
This commit is contained in:
@@ -103,10 +103,34 @@ public class MovieService {
|
||||
* PUT ALL PARSING CODE INSIDE try-catch
|
||||
*/
|
||||
public void displayMoviesByGenre(Long genreId, Long page) {
|
||||
// TODO: Write your code here
|
||||
// All parsing code must be inside try-catch
|
||||
|
||||
String jsonResponse = getMoviesByGenreList(genreId, page);
|
||||
|
||||
if (jsonResponse == null) {
|
||||
System.out.println("Failed to get genres");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Your code here
|
||||
JsonObject rootObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
|
||||
JsonArray moviesArray = rootObject.getAsJsonArray("movies");
|
||||
Type movieListType = new TypeToken<ArrayList<Movie>>(){}.getType();
|
||||
|
||||
Gson gson = new Gson();
|
||||
List<Movie> movies = gson.fromJson(moviesArray, movieListType);
|
||||
int total = rootObject.get("total").getAsInt();
|
||||
int totalPages = rootObject.get("total_pages").getAsInt();
|
||||
|
||||
for(int i=0 ; i<movies.size() ; i++) {
|
||||
|
||||
Movie movie = movies.get(i);
|
||||
System.out.println("ID: " + movie.getId() + " | " + movie.getTitle() +
|
||||
" (" + movie.getYear() + ")");
|
||||
|
||||
}
|
||||
|
||||
System.out.println("\nTotal: " + total + " | " + "Total Pages: " + totalPages);
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("ERROR: " + e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user