Slide 1

Slide 1 text

Retrofiti A Pragmatic Approach to using Retrofit in Android ~ Roger Taracha ~ @thedancercodes

Slide 2

Slide 2 text

About Me ● Software Engineer. ● Python, JS, Android ● Dance Choreographer. ● Traveling. ● Community Development. ● DIY Hobbyist. ● Learning Facilitator/ Lead Android Engineer - Andela

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

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.

Slide 7

Slide 7 text

β€œHigher level libraries reduce your code and make your app run faster and better.” - Pro Tip -

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

TODO 1: Add Required Dependencies ● Add the following dependencies in app/build.gradle: β—‹ Design β—‹ RecyclerView β—‹ Cardview β—‹ Glide β—‹ Retrofit β—‹ Converter-gson β—‹ ConstraintLayout ● Sync the project

Slide 11

Slide 11 text

TODO 2: Set Internet Permission ● Set the Internet Permission in the Manifest file: β—‹ app/src/main/AndroidManifest.xml

Slide 12

Slide 12 text

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/

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

TODO 3: Movie Encapsulation Class and its Response

Slide 15

Slide 15 text

TODO 3: Movie Encapsulation Class and its Response

Slide 16

Slide 16 text

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)

Slide 17

Slide 17 text

TODO 4: Define API Client & Service

Slide 18

Slide 18 text

TODO 4: Define API Client & Service

Slide 19

Slide 19 text

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.

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

TODO 5: Movie Adapter

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

TODO 6: Main Activity

Slide 26

Slide 26 text

Run the App You should see a list of top rated movies

Slide 27

Slide 27 text

Further Reading ● https://futurestud.io/tutorials/retrofit-2-share-okhttp-client-and-convert ers-between-retrofit-instances ● https://guides.codepath.com/android/consuming-apis-with-retrofit ● http://jakewharton.com/making-retrofit-work-for-you/

Slide 28

Slide 28 text

This is the most important takeaway that everyone has to remember.

Slide 29

Slide 29 text

Thank you @thedancercodes TheDancerCodes TheDancerCodes