Calling Web Service with Android Architecture Component

Boonya Kitpitak
The OOZOU Blog
Published in
5 min readJul 21, 2017

Introduction

Since Google IO 2017, Android Architecture Component has been one of the topic that really catches my interest (I guess not only me but also, other Android developers). Right now, there are a lot of coding architectures out there such as MVP, MVVM and etc. However, this time Google has provided the official guideline that helps you architect an Android application. Therefore, it really is worth trying it out. In this article, I will focus on calling web service with this new architecture. The source code of this project is on my Github. Moreover, It’s pure Kotlin :)

Before starting

This article will focus on calling web service with Android Architecture Component. The basic of Kotlin and other library such as Dagger, Retrofit will not be included in this article.

How does architecture look like ?

Inside the red oval is where we are focusing on

The picture above is taken from official Android site. I would like to classify above picture into two layer.

Data Layer

  1. Repository : Talking to local datasource(database) and remote datasource(web service)
  2. Remote datasource : Connecting to web service

Presentation Layer

  1. ViewModel : Communicating to Repository then update the data to UI (fragment/activity) respectively
  2. Fragment/Activity : All about the UI (set up and update)

Demo Application

The demo application fetches the data from https://swapi.co. The application will display list of species in Star War.

screenshot of the app

In this article, I will also cover common actions dealing with web service including getting result, handling error, and showing loading UI. You could clone the code from here and look at it along with this article.

Data Layer

Remote datasource

Retrofit is used as remote datasource to connect to web service. Below code is the interface for the API. If you use Retrofit, you should already be familiar with this.

Source code package: boonya.ben.callingwebservice.api

It’s only include one endpoint for the sake of simplicity.

Repository

The calling to web service happen here, and it also includes receiving data and error from web service. Let’s take a look at the code below.

Source code package: boonya.ben.callingwebservice.species

As you can see that the function getSpecies is higher-order function

A higher-order function is a function that takes functions as parameters, or returns a function.

The getSpecies take two functions as arguments

  1. (successHandler: (List<Species>?) -> Unit ) This function is called when the call to the web service is successful. It takes List<Species> which is the response data from web service as argument. You will see later how ViewModel can access this data
  2. (failureHandler: (Throwable?) -> Unit) This function is invoked when the call to web service is fail. It also takes Throwable as argument which will be manipulated in ViewModel.

By having Repository, the presentation layer (Activity/Fragment, ViewModel) will not have to directly access to remote source. Therefore, if we want to use other remote source instead of Retrofit, we could do with out changing the code in presentation layer.

Presentation Layer

Before going any further, let me introduce you to another new component called LiveData.

Here are some short explanations.

LiveData makes normal model class observable.

MutableLiveData is implementation of LiveData. MutableLiveData can use method called setValue that can be used to assign new value to the LiveData.

ViewModel

ViewModel is new component introduced in Android Architecture Component. Instead of putting everything in Activity/Fragment, we could separate the code that is not related to UI into ViewModel. Moreover, ViewModel survive when configuration changes(rotate screen) occur. Normally, Activity/Fragment and everything inside will be recreated if there is configuration change. However, by using ViewModel, the existed one can be reused with out creating new one.

Let’s take a look at example.

Source code package: boonya.ben.callingwebservice.species

There are three MutableLiveData set up to be observed from activity/fragment.

  1. isLoading: set to true when start calling web service, and change to false when the call is success or fail.
  2. apiError: if there is error in calling web service the Throwable data will be set to this variable.
  3. speciesResponse: when the data is successfully obtain from web service, the response data will be set to this variable.

Calling to Repository from ViewModel

To make Repository calls web service, we should trigger the method called getSpecies. This method will call another method in Repository which I wrote about in Repository section.

As you can see that repository.getSpecies(…. , ….) takes two functions as arguments. The explanations of each argument is down below.

  1. The first argument is the function that will be triggered when calling to the web service is successful. There will be response data returning from web service can be referred to them as it
  2. The second argument function is triggered when calling to the web service is fail and the Throwable data can be referred as it.

Activity

The activity will only contain the code which is related to UI. Here are the code for activity.

To make the activity work with Android Architecture Component, the activity should implement LifecycleRegistryOwner and also override method getLifeCycle() like above.

Let’s take a close look at attachObserver() method. This activity will observe to three MutableLiveData in ViewModel.

  1. viewModel.isLoading: when change occur to this variable showLoadingDialog method will be triggered to show or hide progressBar
  2. viewModel.apiError: Snackbar with error message will show if the change occur to this variable.
  3. viewModel.speciesResponse: The recycler adapter will be notified when data is set to this value

Conclusion

I personally like Android Architecture Component a lot. Not only it allows you to write more testable and more maintainable code but also solve the pain that most of Android developer faces like handling configuration change. If you are about to start new project, it really worth check this architecture out. At the point of writing this article the library is in alpha state. However, after the first stable release everything should be ready to go.

Reference

--

--

Boonya Kitpitak
The OOZOU Blog

Android Developer at Oozou. Also Guitarist and Headbanger