Android Networking: Fundamentals

Sep 6 2022 · Kotlin 1.6, Android 12, Android Studio Chipmunk | 2021.2.1 Patch 1

Part 2: Implement Retrofit Basics

10. Introduction

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 09. Conclusion Next episode: 11. Build Retrofit Components

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 10. Introduction

The student materials have been reviewed and are updated as of July 2022.

Transcript: 10. Introduction

Nice to see you in the second part of this course! :]

In the previous part, you learned about HTTP connections, REST, and the JSON format. You also explored the usage of the Gson parser, a translator between the JSON format, and Kotlin or Java objects. All of these concepts were presented to you through various API calls in the Taskie app.

However, you might have noticed how a good part of the code you were writing in each of the episodes was repeated.

Specifically, the options for the HttpConnection, the way you were writing and receiving data, and the way you started a new thread for the request, and posted the result to the main thread. All of this can change! With a little help from Retrofit! :]

Retrofit is the most popular library for networking in Android, developed by the Square Open Source company. It is in fact the standard for networking in Android, and even Google recommends using it, for all your requests.

Retrofit handles threading for you automatically, it gives you the ability to handle both errors and success cases, without using try/catch blocks, but most importantly, it allows you to easily launch REST method requests, without having to write a ton of boilerplate.

It can also use automatic response and request parsing, with custom converters, which you’ll use later in this part.

To set up Retrofit, you need to give it a Base url for your requests, and depending on the amount of customization you want, custom Http clients, interceptors, parsers, and much more!

Once you build the Retrofit client, you can create any number of classes called ApiServices, which represent the API calls you can make.

Retrofit then generates the required networking code, depending on how the Api services have been written. You’ll see how to implement these services in later episodes.

Finally, when you have a service, you use it to execute or enqueue calls, depending on whether you want it to be blocking, or non-blocking. And you pass in the appropriate callbacks, if needed.

It works more-less the same as your current API calls, but it takes less code, is easier to work with, and it handles a lot of things out of the box, which you used to do manually. I’m really excited to start retrofitting our API calls, and I can imagine you are too! See you in the next episode! :]