Android Reactive Programming with ReactiveX — RxJava2, RxKotlin and RxAndroid work with Retrofit2

Laurence Liu
5 min readDec 17, 2018

How Retrofit2 work with ReactiveX?

I don’t remember how long, but I think it is about three or four years ago. Retrofit2 didn’t provide function for Reactive Programming. But thanks for Jake Wharton who is contribute a lot for Android developer society. He not only create RxJava Adapter for Retrofit2 but also design RxBinding for Android UI widgets.

About two years ago, RxJava Adapter already merge in the Retrofit2 project to let developers use this more easier. We just need to import it with this line and we could do Observable stuff to our data model.

implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit2_version"

I am going to use GitHub API v3 to demonstrate how powerful ReactiveX is it.

Here are three examples we will achieve in this story.

  1. Fetching data with Retrofit and RxJava2 to get some user.
  2. Filtering the user who name is starting with “s”.
  3. Fetching all repos from a user and find the top contributor of each repo.

And here are the requests we need.

Request1: 
https://api.github.com/users?since={number}
Example1:
https://api.github.com/users?since=135
Request2:
https://api.github.com/users/{userName}
Example2:
https://api.github.com/users/octocat
Request3:
https://api.github.com/users/{userName}/repos
Example3:
https://api.github.com/users/octocat/repos
Request4:
https://api.github.com/repos/{userName}/{repoName}/contributors
Example4:
https://api.github.com/repos/octocat/linguist/contributors

Note: I had used ReactiveX as the path {userName}, but there is a limitation of GitHub API request. It is easy to exceed if we use ReactiveX due to the repo number.

I assume that you already know what is RESTful API and have the basic concept of how to use Retrofit2. If you don’t, please check this.

Base setup

build.gradle

Let’s start with the libraries we need to import.

RetrofitClient

Same as before, creating a RetrofitClient to prepare fetching the data.
I didn’t do lot connectTimeout, readTimeout or network exception stuff in this quick sample. Therefore, making sure you’re under the stable network.

GitHubMethods

Once we import RxJava Adapter, the models we build are able to be Observable. Like the README they provide. Single, Observable and Completable are available.

Models

In previous story, I had introduced a useful plugin for Android Studio which is able to create model in a simple way.

The Repo model is custom for the Example 3.

Here are the models we need.

Example 1 and Example 2

GitHubUserAdapter

Basically, this is a piece of cake if we already have the basic concept of RecyclerView and Adapter.

GitHubUserActivity

As usual, executing those steps every time we use ReactiveX.

  • Create Observable (Line: 15)
  • Create Observer (Line: 16)
  • Create Subscribe (Line: 18 to 21)
  • Do Operator (None)
  • Do Dispose (DisposeSingleObserver already do this itself due to the Single Observable)

That is how we create our first example.
Also you may check this story to compare with the same result but without Reactive Programming.

GitHubUserStartWithSActivity

This example is very similar with GitHubUserActivity. The differences are we do Operator and change the Single to Observable. And the result is filtering every user name which is starting with “s”.

I created two similar function with Single and Observable on purpose. But the fact is the code below are doing the same thing due to the fact that Single provides function call toObservable(). This may change Single to Observable.

return RetrofitClient.gitHubMethods().getGitHubUserObservable(135)return RetrofitClient.gitHubMethods().getGitHubUserSingle(135).toObservable()

Let’s move to the Line 21 to 24. There is a combination of RxJava and RxKotlin. Using flatMap and lambda expression to turn GitHubUser to Observable. And the Line 22 githubUser.toObservable() is the part which is using RxKotlin.

Example 3

RepoAdapter

Same as GitHubUserAdapter. I just put two TextView in it. You could custom this whatever you want.

GitHubRepoActivity

We need to spend more time to deal with this tricky example.

As we could see, the purple request which is been called several times.
It is not hard to know what will we do before knowing Reactive Programming.
It may use a loop, a recursive, or anything we could do to call the contributor requesting several times. We also need to deal with Thread or AsyncTask to mange the thread problem.
But everything is getting better when we know ReactiveX.

There is no problem with first request users/{userName}.
Also the second request users/{userName}/repos is not a big deal if we temporary ignore the function replay().
But this is what we need to discuss about, the replay() function.

replay(): Using to let an Observable emits the data on new Subscribe without doing the logic again, which means we don’t have to do the “http Call” motion in our code over and over again.

Due to the function replay() for the gitHubRepoObservable, we have to use connect() to start executing the Observable.

I put all repo name in the list when it executes to the gitHubRepoObservable.
The next step is put every top contributor in the it. If we could not find the top contributor, just put “Null” on it. Therefore, the data will showing “Null” before getting the result.(You could change to ProgressBar if you like.)

The mRepoList will store all repo name when the gitHubRepoObservable emits data. And storing the top contributor data when the repoConObservable emits. The trick part is we have to change Repo data into Observable via flatMap and toObservable()(Line 42). Just similar with the Example 2.

Example 1 and Example 2 are just for warming up. I believe Example 3 is already showing how formidable is Reactive Programming.

ReactiveX is able to do more than this sample. They already provide as many function as they could to satisfy any possible situation. We may do more practice in the future to know more about it.

--

--