Implement movie explorer API features
This commit is contained in:
@@ -106,9 +106,26 @@ public class MovieService {
|
||||
// TODO: Write your code here
|
||||
// All parsing code must be inside try-catch
|
||||
try {
|
||||
// Your code here
|
||||
|
||||
} catch (Exception e) {
|
||||
String jsonResponse = getMoviesByGenreList(genreId, page);
|
||||
if (jsonResponse == null || jsonResponse.isEmpty()) {
|
||||
System.out.println("No response received.");
|
||||
return;
|
||||
}
|
||||
JsonObject rootObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
|
||||
JsonArray moviesArray = rootObject.getAsJsonArray("movies");
|
||||
int total = rootObject.get("total").getAsInt();
|
||||
int totalPages = rootObject.get("total_pages").getAsInt();
|
||||
Type movieListType = new TypeToken<ArrayList<Movie>>() {}.getType();
|
||||
Gson gson = new Gson();
|
||||
List<Movie> movies = gson.fromJson(moviesArray, movieListType);
|
||||
System.out.println("Movies:");
|
||||
for (Movie movie : movies) {
|
||||
System.out.println("ID: " + movie.getId() + " | " + movie.getTitle() + " (" + movie.getYear() + ")");
|
||||
}
|
||||
System.out.println("Total movies: " + total);
|
||||
System.out.println("Total pages: " + totalPages);
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println("ERROR: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user