Android combine Retrofit + RxJava Tutorial

RxJava is the good java library for android development which composing asynchronous and event-based programs. It is a better than AsynTask because it has more control of the thread and more features to use. For retrofit, its supports the asynchronous and synchronize thread but its does not able to get much control of the thread. So we use use rxJava to replace with the retrofit asynchronous method. In this tutorial, I will tell you about android combine retrofit + rxjava in the project so you can be more understand about it.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “RetrofitRxJavaExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Add rxJava, retrofit and gson dependencies

Go to build.gradle (Module:app) and add rxjava,retrofit and gson in the dependencies section. After that click sync so that the library will be added to your project.

Add a Model

Right click your project and create a new model class “ProfileResponses”. Copy paste the code into your model.

Create an Interface

After that, create an interface “ProfileServices” by right click your project name”.

Create a new class

Add one more new class “RetrofitManager” for init the retrofit rest services so you can use and call it in your mainactivity.

Edit activity.main.xml layout

Go and edit the layout of your main activity.

Edit class

Last, navigate to the mainactivity class and follow the source code in the below to perform the asynchronous call in your project.

Run your Project

In conclusion, the rxjava + retrofit is successfully to implement in your project. You now can check it out.

(Android combine Retrofit + RxJava)

Source Code

(Visited 803 times, 1 visits today)

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *