In this blog we are going handle api calls using Rxjava and Retrofit library which makes simple and cleaner way to define API.
RxJava is a Java VM implementation of Reactive Extensions a library for composing asynchronous and event-based programs by using observable sequences.
Retrofit is a REST Client for Android and Java. It makes it relatively easy to retrieve and upload JSON. Retrofit uses the OkHttp library for HTTP requests.
Add the following dependencies to your gradle file
1 2 3 4 5 |
compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' compile 'com.google.code.gson:gson:2.7' compile 'com.squareup.retrofit2:retrofit:2.0.2' compile 'com.squareup.retrofit2:converter-gson:2.0.2' |
Once the dependencies are all set up, We would need an instance of Retrofit to make network calls.
baseUrl-> Set up Base URL of API
addConverterFactory() — converter factory for serialization and deserialization of objects
1 2 3 4 5 6 |
Retrofit retrofit = new Retrofit.Builder() .baseUrl(Constants.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .client(oktHttpClient.build()) .build(); |
We can define post parameter for the API. The APIs are declared in an interface:
1 2 3 4 5 6 7 8 9 |
@FormUrlEncoded @POST(HOME_PAGE_API) Observable<HomeDataModel> gethomedataRx( @Field("width") String width, @Field("show_category") int category, @Field("count") String count, @Field("mfactor") String mfactor, @Field("wk_token") String wk_token ); |
In Rx, the subscriber implements three methods to interact with observable:
- onNext(Data): Receives the data from the observable
- onError(Exception): Gets called if an exception is thrown
- onCompleted(): Gets called when the data stream ends
For Android, to subscribe to an observable, we first tell the observable about the threads on which we plan to subscribe to and observe.
Making an API call:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Observable<HomeDataModel> observable = ApiClient.getClient(mContext).create(ApiInteface.class) .gethomedataRx(getScreenWidth(),0, "4", "1", SharedPreference.getWkToken(mContext, Constants.CUSTOMER_SHARED_PREFERENCE_KEY_WK_TOKEN)); observable.subscribe(new Observer<BaseModel>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(BaseModel value) { Log.d("RxJAVACall", "onNext: --------->" + value); } @Override public void onError(Throwable e) { } @Override public void onComplete() { Log.d("RxJAVACall", "DONE: --------->"); } }); |
That’s it, we can use the Observable/Flowable returned by the API and subscribe() to it to handle the data
Merging the multiple requests in Rxandroid
Merge operator combine multiple observable into one
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Observable.merge(Observable1,Observable2).subscribe(new Observer<BaseModel>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(BaseModel value) { Log.d("RxJAVACall", "onNext: --------->" + value); } @Override public void onError(Throwable e) { } @Override public void onComplete() { Log.d("RxJAVACall", "DONE: --------->"); } }); |