Sachin Kumar
Aug 25, 2017 · 4 min read

RxJAVA With Retrofit Documentation

Author: Sachin Kumar

Sample App:

In this app we have one button Hit Service and one textview which we will show one text i.e firstname after fetching it from service.

Step 1 : To get started first add the following dependencies in the app build.gradle :

Step 2 : Suppose we have a following service to hit in our module URL is: https://reqres.in/api/users/2 and its response is something like this :

Suppose in the above response we want to retrieve first_name which is nested in JSON object named data. So, in this scenario we have to create two Model classes. For this go to : http://pojo.sodhanalibrary.com/ and paste the above response in the field “Enter JSON or XML here” and name the POJO class “Model”:

And it will generate the following two classes Model.java and Data.java and number of classes depends on the level of nesting in the response.

Copy the above two classes and paste in our Model package of our app and remove the setter methods from the code because it will be handled by retrofit.

Step 3 : Now, add the following annotation @SerializedName(“”) on every variable in both the model classes and remember it should be similar to the key in the JSON response.

Step 4 : Now time is to create a class named ApiClient.java ,it will create the instance of Retrofit using BASE URL and here we are using GSON converter for JSON parsing and for call back we are using RxJAVA.

We can also refer to the commented code above for the implementation without RxJAVA.But here we are focusing on Retrofit with RxJAVA.

Step 5 : Now, we are creating an interface called ApiInterface.java ,it is responsible for managing all the request from the service ,here we are creating one abstract method with one annotation above indicating type of HTTP request we are making with the last part of BASE URL and this method has a return type Observable<Model> which is in RxJAVA context a class emits an event or data to the Observer on the condition if Observer is Subscribed to receive the event.

Step 6 : In this step we are taking the call back using RxJAVA in our onClicklistener when we press the button Hit Service in our Sample app.

In the above code snippet first we are creating an instance of ApiInterface and after that getting an data from JSON response above and storing it into an Observable<Model> of type Model class and using it reference we are subscribing on a new background thread and Observing on main/UI thread ,Observer in this case overrides three methods:

  • onNext() : It has an argument type Model class through which we are accessing the first_name from the JSON response and setting it to textview and it is called first every time after every success response.
  • onCompleted() : It is called after onNext() and here we are displaying a toast indicating view has been updated.
  • onError() : It will be called when some error occurs because of any reason.

Step 7 : This step is not the part of the current implementation but still we are discussing the call back without RxJAVA.

The methods above are taking care of the failure cases related to the service only but there are many cases or errors that may occur during this operation but sadly these will not be taken care by them.

That’s the reason it is a good practise to have RxJAVA with Retrofit.

Step 8 : For code base take a clone from my GIT HUB URL : https://github.com/sachin1kumar/RxJAVARetrofit.git

)

Sachin Kumar

Written by

Senior Android Developer | ML Enthusiast

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade