newThread(): Creates and returns a Scheduler that creates a new Thread for each unit of work.
So it’ll run your code after the current task on the thread is complete. trampoline(): This runs the tasks on the current thread.We can pass any of the following Schedulers in it. SubscribeOn() creates a Scheduler thread over which we do the network call. subscribe(this::handleResults, this::handleError) observeOn(AndroidSchedulers.mainThread()) Observable cryptoObservable = cryptocurrencyService.getCoinData("btc") ĬryptoObservable.subscribeOn(Schedulers.io()) Creating a Single Call Using RxJava CryptocurrencyService cryptocurrencyService = retrofit.create(CryptocurrencyService.class) Using the magic of RxJava we’ll set a value on this field to transform the response. Public class Ticker String String String String String List markets = null ĬoinName is a field we’ve set. Public class Market String String Float volume Public class Crypto Ticker Integer Boolean String error Note: The parameter name must match with the one in the The POJO class Crypto.java is given below: package String BASE_URL = String passes in the path we specify into the curly braces. The code for the CryptocurrencyService.java class is given below. The code for the layout activity_main.xml is given below. Implementation '3:logging-interceptor:3.9.1' Implementation 'io.reactivex.rxjava2:rxandroid:2.0.1' Implementation 'io.reactivex.rxjava2:rxjava:2.1.9' Project StructureĪdd the following dependencies in our adle file: implementation ':cardview-v7:27.1.0' Let’s see how it’s done through our sample application below. To do multiple calls or transform the response, we use RxJava operators. Use Observable type in the interface instead of Call.To use RxJava in retrofit environment we need to do just two major changes:
RxAndroid is an extension of RxJava and it contains the Android threads to be used in the Android Environment. Schedulers in RxJava is responsible for performing operations using different threads. A background thread for the network call and the main thread for updating the UI. RxJava is a library that is used for asynchronous and reactive programming in the form of streams. HttpLoggingInterceptor is used to log the data during the network call. addConverterFactory(GsonConverterFactory.create(gson)) Retrofit retrofit = new Retrofit.Builder() OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build() This is how a Retrofit instance is created: HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor() Retrofit is a REST client that uses OkHttp as the HttpClient and JSON Parsers to parse the response. We’ll be using Java 8 in our Android Application to unleash lambda expressions. Transforming the Retrofit POJO response using RxJava.Doing Multiple Retrofit Calls Using RxJava.We’ll be creating an application that populates a RecyclerView using Retrofit and RxJava. In this tutorial, we’ll be implementing Retrofit calls using RxJava in android app.