Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Retrofiti 👍- A Pragmatic Approach to using Retr...

Roger Taracha
February 17, 2018

Retrofiti 👍- A Pragmatic Approach to using Retrofit in Android

These are slides from my talk at the Android254 Meet Up.
https://www.meetup.com/Android254/events/247151856/

Roger Taracha

February 17, 2018
Tweet

More Decks by Roger Taracha

Other Decks in Technology

Transcript

  1. About Me • Software Engineer. • Python, JS, Android •

    Dance Choreographer. • Traveling. • Community Development. • DIY Hobbyist. • Learning Facilitator/ Lead Android Engineer - Andela
  2. Choosing a HTTP Client: (1) HttpURLConnection In HttpUrlConnection is an

    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.
  3. Choosing a HTTP Client: (2) Square’s OkHttp • Used by

    Android internally, can also be used explicitly. • Supports connection pooling and response caching. • Replaces HttpUrlConnection; mimics its API.
  4. Choosing a HTTP Client: (3) Google’s Volley • Volley is

    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.
  5. Choosing a HTTP Client: (4) Square’s Retrofit • Popular HTTP

    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.
  6. “Higher level libraries reduce your code and make your app

    run faster and better.” - Pro Tip -
  7. Project Setup • Version of Android Studio and gradle being

    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
  8. TODO 0: Setup API Key • Set up the API

    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.
  9. TODO 1: Add Required Dependencies • Add the following dependencies

    in app/build.gradle: ◦ Design ◦ RecyclerView ◦ Cardview ◦ Glide ◦ Retrofit ◦ Converter-gson ◦ ConstraintLayout • Sync the project
  10. TODO 2: Set Internet Permission • Set the Internet Permission

    in the Manifest file: ◦ app/src/main/AndroidManifest.xml
  11. TODO 3: Movie Encapsulation Class and its Response • Get

    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/
  12. TODO 3: Movie Encapsulation Class and its Response • Movie

    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.
  13. TODO 4: Define API Client & Service • The Client

    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)
  14. TODO 5: Movie Adapter • An Adapter is a bridge

    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.
  15. TODO 6: Main Activity • Create initViews() helper method. ◦

    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