Work with Retrofit
Kotlin and Android Development featuring Jetpack — by Michael Fazio (79 / 125)
👈 Create a Database | TOC | Load Data from External APIs 👉
While we’ve gotten away with developing offline apps so far, that’s very rarely the case in the real world. Don’t get me wrong, everything we’ve done so far is valuable (both here and with Penny Drop), but calling APIs is a critical piece of building an app.
If you’re not familiar, Retrofit[9] is an open source library created by Square, Inc.,[10] and has become a de facto “official” Android library — so much so that the official Google tutorials use Retrofit for any API calls.
I’m going to cover the basics with setting up a Retrofit service here. If you’re more interested in just using the service rather than building it, skip ahead to the next section: Add the ABL API Client. We’re going to be using the ABL API client for our app rather than what I’m showing you here, so you won’t actually be using the code we write. But as Retrofit is so widely used, I figure it’s worth covering.
Build a Retrofit Service
The core piece of a Retrofit implementation is the Service interface. Here, you define each of your APIs by listing the HTTP method (GET, POST, and so on), the API path, any query parameters, and the return type. Again, the great thing about Retrofit is that you’re always working with objects rather than having to manually convert things…