Implementing RxJava2 & Retrofit2 for better performance during API calls

Tristan Larsin
AndroidPub
Published in
3 min readJul 12, 2018

--

API handling can be tedious if not completed in a good way. Loading data into an Android app can lag the UI thread if not done on a separate thread. RxJava2 and Retrofit2 are very powerful libraries that solve this problem. However, when you first get started with these libraries it can be very difficult to understand how to implement them in your application. I struggled with how to implement them and that is the sole reason I am writing this tutorial.

For this example we are going to use an API that returns cryptocurrency data. You can click on this link to view the JSON it returns: https://min-api.cryptocompare.com/data/all/coinlist . However, here is the data we are interested in.

JSON Response

We will need two different POJO (Plain Old Java Object) classes. A great website that will provide you with the entire class laid out is JSONSchema, and all you need to do is paste the raw JSON in its editor.

The first POJO class will be CoinList.java for the entire response JSON. The second will be CryptoData.java which will be the data under the object Data inside the response.

Here is the CoinList.java POJO class.

CoinList POJO Class

Now for the CryptoData.java POJO Class.

CryptoData POJO Class

Now add the dependencies for the libraries to your build.gradle. We are going to use RxJava2, Retrofit2, GSON, and a Retrofit2-RxJava2 Adapter.

// https://github.com/ReactiveX/RxAndroid                           implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'                           implementation 'io.reactivex.rxjava2:rxjava:2.1.10'// Retrofit http://square.github.io/retrofit                           implementation 'com.squareup.retrofit2:retrofit:2.4.0'                           implementation 'com.squareup.retrofit2:adapter-rxjava:2.4.0'                           implementation 'com.squareup.retrofit2:converter-gson:2.4.0'                           implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'// GSON body parser
implementation 'com.google.code.gson:gson:2.8.2'

Now that we have all the dependencies we can start implementing RxJava2 & Retrofit2. First off we will create our class that initializes Retrofit. We have to make sure we add the .addConverterFactory(GsonConverterFactory.create()) inside the getClient() method, this converts the response JSON into GSON and then into the POJO objects. We also need to make sure to add the .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) method, this verifies we are using RxJava2 for this API call.

CryptoCompareAPI initialization of Retrofit

Now we will call getClient() and utilize RxJava2 to run this on another thread and as you can see it is actually the AndroidSchedulers.mainThread(). This call will grab all the data at make objects out of it using the POJO classes and input it into the allCurrencyList variable List. Once this call is completed, inside the onComplete() method it calls another method that will update the UI. This is done by utilizing the MVP Architecture, therefore I was calling the Presenter to update the View from the Model.

RxJava Observation & Subscribe

Let me know what you think of this article down in the comments!

Give my Twitter a follow if you would like to keep up with my current projects. Do you invest in Cryptocurrencies? Check out my new app in Beta, Veox — Crypto currency portfolio tracker!

--

--

Tristan Larsin
AndroidPub

Founder/Executive Director @IDISCY & Android @Weedmaps , Previously @ScanwellHealth , @Branch