Last active
August 29, 2015 14:16
-
-
Save billmote/7380686bbda4e3656b25 to your computer and use it in GitHub Desktop.
A custom TypeAdapter to handle the case when Rotten Tomatoes returns "" rather than a number when runtime is null.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Parcel | |
| @DebugLog | |
| public class Movie { | |
| @Expose | |
| public String id; | |
| @Expose | |
| public String title; | |
| @Expose | |
| public Integer year; | |
| @SerializedName("mpaa_rating") | |
| @Expose | |
| public String mpaaRating; | |
| @SerializedName("critics_consensus") | |
| @Expose | |
| public String criticsConsensus; | |
| @SerializedName("release_dates") | |
| @Expose | |
| public ReleaseDates releaseDates; | |
| @Expose | |
| public Ratings ratings; | |
| @Expose | |
| public String synopsis; | |
| @Expose | |
| public Posters posters; | |
| @SerializedName("abridged_cast") | |
| @Expose | |
| public List<AbridgedCast> abridgedCast = new ArrayList<AbridgedCast>(); | |
| @SerializedName("alternate_ids") | |
| @Expose | |
| public AlternateIds alternateIds; | |
| @Expose | |
| public Links links; | |
| /* | |
| This field must not match the JSON response so that we can manually parse the data. See the | |
| RestClient for details. | |
| */ | |
| public Integer movieRuntime; | |
| public String getId() { | |
| return id; | |
| } | |
| public void setId(String id) { | |
| this.id = id; | |
| } | |
| public String getTitle() { | |
| return title; | |
| } | |
| public void setTitle(String title) { | |
| this.title = title; | |
| } | |
| public Integer getYear() { | |
| return year; | |
| } | |
| public void setYear(Integer year) { | |
| this.year = year; | |
| } | |
| public String getMpaaRating() { | |
| return mpaaRating; | |
| } | |
| public void setMpaaRating(String mpaaRating) { | |
| this.mpaaRating = mpaaRating; | |
| } | |
| public Integer getMovieRuntime() { | |
| return movieRuntime; | |
| } | |
| public void setMovieRuntime(Integer movieRuntime) { | |
| this.movieRuntime = movieRuntime; | |
| } | |
| public String getCriticsConsensus() { | |
| return criticsConsensus; | |
| } | |
| public void setCriticsConsensus(String criticsConsensus) { | |
| this.criticsConsensus = criticsConsensus; | |
| } | |
| public ReleaseDates getReleaseDates() { | |
| return releaseDates; | |
| } | |
| public void setReleaseDates(ReleaseDates releaseDates) { | |
| this.releaseDates = releaseDates; | |
| } | |
| public Ratings getRatings() { | |
| return ratings; | |
| } | |
| public void setRatings(Ratings ratings) { | |
| this.ratings = ratings; | |
| } | |
| public String getSynopsis() { | |
| return synopsis; | |
| } | |
| public void setSynopsis(String synopsis) { | |
| this.synopsis = synopsis; | |
| } | |
| public Posters getPosters() { | |
| return posters; | |
| } | |
| public void setPosters(Posters posters) { | |
| this.posters = posters; | |
| } | |
| public List<AbridgedCast> getAbridgedCast() { | |
| return abridgedCast; | |
| } | |
| public void setAbridgedCast(List<AbridgedCast> abridgedCast) { | |
| this.abridgedCast = abridgedCast; | |
| } | |
| public AlternateIds getAlternateIds() { | |
| return alternateIds; | |
| } | |
| public void setAlternateIds(AlternateIds alternateIds) { | |
| this.alternateIds = alternateIds; | |
| } | |
| public Links getLinks() { | |
| return links; | |
| } | |
| public void setLinks(Links links) { | |
| this.links = links; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Created by billmote on 9/7/14. | |
| */ | |
| @DebugLog | |
| public class RestClient { | |
| private ApiService apiService; | |
| public RestClient(String endpoint, boolean enableLogging) { | |
| Gson gsonBuilder = new GsonBuilder() | |
| .registerTypeAdapterFactory(new ItemTypeAdapterFactory()) | |
| /* | |
| Rotten Tomatoes returns an int when there's a valid value for runtime, but they | |
| return an empty string when the value is null. Because they return different | |
| JSON primitive types we have to create a custom TypeAdapter for our Movie.class. | |
| */ | |
| .registerTypeAdapter(Movie.class, new JsonDeserializer<Movie>() { | |
| @Override | |
| public Movie deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { | |
| JsonObject movieObject = arg0.getAsJsonObject(); | |
| Gson gson = new Gson(); | |
| Movie movie = gson.fromJson(arg0, Movie.class); | |
| int runtime; | |
| try { | |
| runtime = movieObject.get("runtime").getAsInt(); | |
| } catch (Exception e) { | |
| runtime = 0; | |
| } | |
| movie.setMovieRuntime(runtime); | |
| return movie; | |
| } | |
| }) | |
| .setDateFormat("yyyy-MM-dd") | |
| .setPrettyPrinting() | |
| .excludeFieldsWithoutExposeAnnotation() | |
| .create(); | |
| RestAdapter restAdapter = new RestAdapter.Builder() | |
| .setLogLevel(BuildConfig.DEBUG && enableLogging ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE) | |
| .setEndpoint(endpoint) | |
| .setConverter(new GsonConverter(gsonBuilder, "UTF-8")) | |
| .setRequestInterceptor(new SessionRequestInterceptor()) | |
| .build(); | |
| apiService = restAdapter.create(ApiService.class); | |
| } | |
| public ApiService getApiService() { | |
| return apiService; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment