Api call by using Retrofit, RxJava and RxAndroid in Kotlin Android

In building the Android apps, Retrofit is the most popular for networking API call. It is very simple and cleaner way API call in the Android application. Retrofit used OkHttp request call which is extended and customized of Http request.  It gives much better performance in comparison with another library like Volley.

Android developers love this because it is pretty simple and easy to integrate into an android application.  Retrofit is not only simple to integrate but also provide many features like handling multiple requests, caching, and easy to handle the error.  Great 🙂

I explained in one of my tutorial blogs about how to use retrofit in Android. I recommended to checking this post Retrofit with RxAndroid.

In this tutorial, we will learn how to use the retrofit API call in Kotlin by using  RxJava and RxAndroid. Retrofit works very seamlessly with RxJava. RxAndroid provides the Android Scheduler for scheduling the main thread switch between the threads easily.  Let’s see how to integrate Retrofit in Kotlin?

In the first step, we need to add Retrofit, RxJava, and RxAndroid dependencies into a build.gradle file in app level. Here are dependencies.

Now we need to create the ApiService class to retrofit initialized.

Here is the sample JSON model.

We need to create the ApiServiceInterface for post and query for networking and model class or POJO for parsing JSON into a plain Java class.

Here is ApiServiceInterface class.

Now the API call for networking by using Retrofit is ready now.  Ohh that’s pretty simple right 🙂

Ok Now my target is not only to create and initialized retrofit but also want to show a demo to fetch some data from mockable io server.  Once I fetched data from server lets shows those data into RecyclerView.  Here I am calling retrofit.

I have implemented this into presenter class. Here is detail.

Retrofit help us for parsing data in a very simple way using Gson Library.  Now we got the Observable<Friends> data to do on next to show on View.

Now we can load this data into recycler view. I used recycler view in FriendsListFragment class.

Wrapping Up: As we practiced that retrofit is very simple and easy to integrate into Android for building awesome Android apps. We can do a lot with retrofit and Reactive Rx. You can play with more to add more features.

Here is the Github link of source code.

In my next tutorial, we can learn how to use Anko Db with a retrofit for offline data sync in Kotlin. If you want to learn basic concepts of Kotlin then I recommended to checking all post here Basic of Kotlin get started in Android part- 1,  part -2,  part -3 and part -4.  Great 🙂

Please do subscribe your email to get every newsletter from this blog and if you feel that this post helps you then do not forget to share and comment below.

Happy Coding 🙂

Sunil Gupta

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

You may also like...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.