Android SDK HTTP Client. PROS: • Low level code; can do anything • Supports all basic HTTP features. • In active development & maintenance by the Android Dev team. CONS: • Can require more code than higher level libraries.
built on top of HttpUrlConnection or OkHttp. • Support Parallel requests -> Multiple requests over the web. • Uses Gson library to decode JSON-formatted content. • Handles device configuration changes elegantly. ◦ EXAMPLE: If the device orientation changes in the middle of request/response cycle, the cycle won’t be interrupted.
Client Library for Android as a result of its simplicity and its great performance • Automatically decodes JSON content to typed objects. ◦ Saves the decoded content as strongly typed objects • Retrofit lets you define your web service calls in Java interfaces. ◦ You then use those interfaces to make requests and handle the responses. • Custom Converters and other features ◦ Includes classes and interfaces to convert data from one format to another.
used. ◦ Android Studio 3.0.1 ◦ Gradle 3.0.1 • Clone the repo and check out to the start branch: ◦ http://bit.ly/2o9ymmo ◦ Check out to the 00-Retrofiti-Exercise branch • Get API KEY from TheMovieDB: ◦ https://www.themoviedb.org/ • APPROACH: Package by Feature
KEY in secrets.properties file. ◦ Create the file in the root folder of your project ◦ You need to secure your API key without pushing to remote repository ◦ NB: This file is added to the .gitignore file.
JSON Response from this url: ◦ http://api.themoviedb.org/3/movie/top_rated?api_key=YOUR_API_KEY • Use this JSON Viewer to beautify the JSON Response: ◦ https://codebeautify.org/jsonviewer • Use JsonSchema2Pojo tool to convert the JSON Response to POJO classes: ◦ http://www.jsonschema2pojo.org/
class contains Getter and Setter Methods for the app. • Gets & Sets the key value pairs for the main movie information. ◦ (ie) poster_path, overview, release_date, genre_ids etc. • TODO 3a: Create a Movie Constructor • TODO 3b: Add an Image URL Field • TODO 3c: Append the Image URL Field ◦ Append to getPosterPath() • NOTE: See images in the next slide.
contains code that instantiates a new Retrofit instance. ◦ The Retrofit object requires the base URL -> the base on which these relative paths will be appended. ◦ This is where the configuration lies • The Service contains the interface ◦ Use interface as source of truth and add a little metadata to it to describe what the actual corresponding requests should look like. ◦ Annotate method with the API path we want to hit @GET(“/movie/popular/”) ◦ Wrap the request in a Call (A request wrapped up in an object that you can either execute synchronously or asynchronously)
between an AdapterView and the underlying data for that view. • Handle onClick listeners on every card in the grid layout of the RecyclerView. ◦ Passing Extras into the Intent to another Activity (Detail Activity). • JSON API calls with image caching were made seamlessly easy using Retrofit and Glide.
This method initialises the Views. • Create loadJson() helper method. ◦ This method handles loading of JSON ◦ This process runs in a background thread that doesn't affect the UI Thread. • Use try/catch for error handling. ◦ Check for an empty API KEY