Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. package movies.test.popularMovies.MovieTools;
  2.  
  3. import org.json.JSONArray;
  4. import org.json.JSONException;
  5. import org.json.JSONObject;
  6.  
  7. import java.text.DateFormat;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.ArrayList;
  11.  
  12. import movies.test.popularMovies.Utilities.Constants;
  13.  
  14. /**
  15. * Created by ZCP on 6/26/17.
  16. */
  17. public final class JsonMovieUtility {
  18. private static final String DATA_CONTAINER = "results";
  19.  
  20. public static ArrayList<MoviePojo> generateParsedMovieDataFromJson(String movieJsonStr) throws JSONException {
  21. JSONObject movieJson = null;
  22. JSONArray movieArray = null;
  23. ArrayList<MoviePojo> movieList = null;
  24. if (movieJsonStr != null) {
  25. movieJson = new JSONObject(movieJsonStr);
  26. }
  27. if (movieJson != null) {
  28. movieArray = movieJson.getJSONArray(DATA_CONTAINER);
  29. MoviePojo movieElement;
  30. movieList = new ArrayList<>(movieArray.length());
  31. for (int i = 0; i < movieArray.length(); i++) {
  32. movieElement = getMovieFromJSON(movieArray.getJSONObject(i));
  33. movieList.add(movieElement);
  34. }
  35. }
  36. return movieList;
  37. }
  38.  
  39. public static MoviePojo getMovieFromJSON(JSONObject movieJSON) {
  40. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  41. try {
  42. MoviePojo movie = new MoviePojo();
  43. movie.votes = movieJSON.getInt("vote_count");
  44. movie.id = movieJSON.getInt("id");
  45. movie.voteAvg = movieJSON.getDouble("vote_average");
  46. movie.title = movieJSON.getString("title");
  47. movie.originalTitle = movieJSON.getString("original_title");
  48. movie.posterPath = Constants.MOVIE_POSTER_BASE_URL + movieJSON.getString("poster_path");
  49. movie.backdropPath = Constants.MOVIE_IMAGE_BASE_URL_PATH + movieJSON.getString("backdrop_path");
  50. movie.overview = movieJSON.getString("overview");
  51. movie.releaseDate = dateFormat.parse(movieJSON.getString("release_date"));
  52. return movie;
  53. } catch (JSONException | ParseException e) {
  54.  
  55. }
  56. return null;
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement