Android Network call with Retrofit and Kotlin’s Coroutines

Balaji
3 min readMar 24, 2020

--

In recent days, Kotlin’s Coroutines are used for asynchronous programming in android to avoid boilerplate codes. In this article, we are going to build a small fully functional app in MVVM pattern using the libraries Retrofit, Kotlin Coroutines and Android Architecture Components.

In case if you haven’t heard about any of the libraries mentioned above, here is the purpose of each library how it is going to use in our app.

  1. Retrofit: Make REST API calls
  2. Moshi: JSON parsing
  3. Coroutines: Asynchronous programming
  4. Android Architecture Components: MVVM pattern

What app we are going to make

In this article we are going to create an app that gets a random quote from REST API and presents it in the Activity as below drawing. The button on the bottom makes an API call and refreshes the quote.

Which REST API we are going to use

For this tutorial, we are going to use the following open REST API. Every time when we call this API, it returns a random quote and its author name in response as JSON. The sample JSON response also is given below.

https://quote-garden.herokuapp.com/quotes/random

{
"_id": "5d91b45d9980192a317c92b9",
"quoteText": "You are the only person on earth who can use ...",
"quoteAuthor": "Zig Ziglar"
}

Cool. At this point, I believe we are good with the requirement for what needs to be done. Let’s start our coding.

Add Dependencies

First of all, we have to add the dependencies for all the libraries mentioned above to our project. We can go to our build.gradle file for the Module: app and add the following dependencies. Also, apply the “kotlin-kapt” plugin at the top.

Model Class

In this step, we create a model class that can accommodate the data available in the JSON response we saw above.

Couple of annotations we have used here. @JsonClass(generateAdapter = true) will generate a JsonAdapter to handle serialization from JSON to Kotlin object and vice-versa. The @filed:Json(name = “value”) defines the JSON key name for serialization against the property it is annotated.

API Interface and Service

In this section, we create an interface for defining our REST API endpoints and Retrofit service object.

Define API Endpoint

In the API interface, the method getRandomQuote is added with a modifier suspend so that it can be executed by a coroutine.

  • A function defined with modifier suspend is called suspending function.
  • A suspending function can call another suspending function or normal function.
  • But a normal function can’t call suspending function directly.
  • Coroutine builders are used to launching a suspending function from normal function. We will see it in detail later in this article.

The following code snippet highlights the key difference in defining API endpoint function in the usual way, RxJava and Kotlin Coroutine.

// usual Retrofit 
fun getRandomQuote(): Call<Quote>
// RxJava
fun getRandomQuote(): Observable<Quote>
// Coroutine
suspend
fun getRandomQuote(): Quote

ViewModel Class

The definition of coroutine from official documentation is,

Coroutines are light-weight threads, they are launched with launch coroutine builder in a context of some CoroutineScope.

Here fetchRandomQuote method launches a coroutine in viewModelScope. Once API call is executed and the quote object is available it is handover to LiveData object liveQuote using postValue method.

The fetchRandomQuote method is called on init method to fetch and show a quote on UI when a ViewModel object is created in the Activity class.

Activity Class & layout xml

Layout XML and Activity class above with comments are self-explanatory.

Demo

That’s it in this article. Let me try to enhance this application with more concepts in another article.

--

--