DEV Community

Cover image for Kotlin RxJava Retrofit tutorial
Odhiambo Paul
Odhiambo Paul

Posted on • Updated on


Kotlin RxJava Retrofit tutorial

Kotlin Retrofit Rxjava

This is a continuation of the previous tutorial where we made network calls
using retrofit and kotlin. Go through the first tutorial to continue.

What is RxJava

Reactivex is a library for composing asynchronous and event based programs
by using observable sequences. It frees you from tangled webs of callbacks,
and thereby makes your code more readable and less prone to bugs.
With Retrofit we can incorporate RxJava to make our code better and our application more efficient. Retrofit also comes with an adapter for including RxJava in our network calls.


//RxJava adapter
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.0'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.12'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
Enter fullscreen mode Exit fullscreen mode

The Interface

Nothing much changes in the interface containing our endpoints. It's only
that we change the return of our functions from call to observable.

What are these Observables

An observable is a class that gives us data or events. It is used to perform
actions and give out the results.




import io.reactivex.Observable
import retrofit2.http.GET
import retrofit2.http.Query

interface TmdbEndpoints {

    fun getMovies(@Query("api_key") key: String): Observable<PopularMovies>

Enter fullscreen mode Exit fullscreen mode

The Service Class

Next we add the RxJava adapter to our retrofit object to include RxJava to our network
network calls and allow us to create the observable when making the call.




import com.odhiambopaul.movies.util.BASE_URL
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

object ServiceBuilder {
    private val client = OkHttpClient

    private val retrofit = Retrofit.Builder()

    fun buildService(): TmdbEndpoints {
        return retrofit
Enter fullscreen mode Exit fullscreen mode


Now it is time to make the network call. First we create a CompositeDisposable.

A CompositeDisposable is a class that helps us to manage all our disposables.
Disposable are the connections between Observable and observer. When we
create observables, and they emit data, a stream is created which later needs to be removed it takes up resources. This stream is the called a disposable. Since
you can create more than one disposable , it would be tiresome to clear them one by one, hence we use a CompositeDisposable which will contain all the disposables created and they can be cleared by just clearing the CompositeDisposable. We use the add() method to add one disposable in our activity. We use the subscribeOn() and/or with the observeOn() method to specify the threads on which the call should be executed on. If we specify the subscribeOn() only, we tell the app to run the call on the current thread. Then we call the subscribe() method to getthe data or error.


Inside the subscribe() method we have two lambda expressions. The first oneis always executed if our call was successful and it returns our value i.e PopularMovies object,then we call the onResponse function that we create to handle our response. If there is an error in our application then the second expression is executed to return a throwable then we call a function in our case, onFailure



package com.odhiambopaul.movies.ui.home

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.odhiambopaul.movies.R
import com.odhiambopaul.movies.util.api_key
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        val compositeDisposable = CompositeDisposable()
                .subscribe({response -> onResponse(response)}, {t -> onFailure(t) }))


    private fun onFailure(t: Throwable) {
        Toast.makeText(this,t.message, Toast.LENGTH_SHORT).show()

    private fun onResponse(response: PopularMovies) {
        progress_bar.visibility = View.GONE
        recyclerView.apply {
            layoutManager = LinearLayoutManager(this@MainActivity)
            adapter =
Enter fullscreen mode Exit fullscreen mode

With RxJava we have managed to reduce the number of lines of code and the resultant code is clean and readable.

The full project can be found in my repo at Movies

Top comments (0)