Android MyShows App — RxAndroid MVVM LiveData ViewModel DataBinding, Networking with Retrofit, Gson & Glide

This Article is all about implementing REST API in the Android app using Retrofit MVVM LiveData ViewModel DataBinding RxAndroid, Android Architecture Components introduce by Google. which make our code more clear and reusable and easy to modify if needed.

Manoj Kumar
7 min readJul 8, 2021

We will create a demo App(MyShows) using these APIs and Componets.

Model-View-ViewModel :

· Model is used to write Business logic. The model represents the actual data and/or information we are dealing with.

· View is used to consume data/result received from ViewModel and inform ViewModel of user interaction.In simple way, we can say that it is used to display data to user.

· ViewModel works as a bridge between Model and View. It is responsible to process data using Model and send back the result to View to consume it. It contains UI Logic that involving both View and Model.

RxJava:

· RxJava is a Java VM implementation of Reactive Extensions. It has become the single most important skill for Android development.

RxAndroid:

· RxAndroid is a library which is specific to the Android platform. RxAndroid library adds few classes on top of RxJava that makes writing reactive components in Android applications easy and hassle-free

RxAndroid works on Observer pattern. It has basically three major component.

· Observable emits a stream of data or events. i.e. a class that can be used to perform some action, and publish the result.

· Subscriber is an Observer that can also unsubscribe from that data source

· Observer receivers the events or data and acts upon it. i.e. a class that waits and watches the Observable, and reacts whenever the Observable publishes results.

Retrofit:

· Retrofit is a REST Client for Java and Android. It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based webservice. In Retrofit you configure which converter is used for the data serialization. Typically for JSON you use GSON, but you can add custom converters to process XML or other protocols. Retrofit uses the OkHttp library for HTTP requests.

Let’s start building an app using these APIs and Components:

· We need to add following dependencies in our app gradle.

· Let’s create Client.java which implement Retrofit and Okhttp:

·HttpLoggingInterceptor is used to take logs from Api calls.

·OKHttpClient is used to create network client for retrofit.

·BASE_URL= https://api.tvmaze.com/ which provide free and clean RESTful API.

·We need to register a call adapter factory with Retrofit that can create RxJava types.

· We need to add convert factory with GSON which provide GSON based response.

· We need to create Service.java interface which consume the REST API.

· @GET annotation = API interface methods.

· @Path annotation = Named replacement in a URL path segment.

· For example:

· We have BASE_URL= “https://api.tvmaze.com/”

· Using GET method it will be “https://api.tvmaze.com/schedule/full”.

· If we provide Path according to api then it will be “https://api.tvmaze.com/shows/1234

· 1234 is a ShowId which we are receiving from getShowById().

· Single is Observable which is provided by RxAndroid.

· Show,ShowDetail &Cast are GSON class which parse JSON into simple java object.

· @SerializeName annotation is needed for Gson to map the JSON keys with our fields. In keeping with Java’s camelCase naming convention for class member properties, it is not recommended to use underscores to separate words in a variable.

· @Expose annotation indicates that this member should be exposed for JSON serialization or deserialization.

Let’s implement Repository:

· We have created ShowRepository.java class which handle the api call. It will return Single Observable to ViewModel.

· We will instantiate Client.java here to connect Service.java interface which provide the REST Api’s methods.

Let’s implement ViewModel:

· We have created ShowsViewModel.java class. In which we will subscribe Single Observable and set the value in MutableLiveData.

· CompositeDisposable is just a class to keep all your disposables in the same place to you can dispose all of them at once.

· MutableLiveData is just a class that extends the LiveData type class. MutableLiveData is commonly used since it provides the postValue() , setValue() methods publicly, something that LiveData class doesn’t provide.

· subscribeOn() at the start of an Observable chain means the process is still operating on a single thread and emitting items synchronously

· onserveOn() simply changes the thread of all operators further downstream.

Let’s implement View:

· We have created ShowsActivity.java class which observe the getShowListLiveData() method from ShowsViewModel class.

· We have used data binding here so we got ActivityShowsBinding auto generated class.

· Inflate layout via DataBindingUtils.setContentView()

· Instantiated view model via ViewModelProviders’s factory method.

· getShowList() which observe livedata from viewmodel and we got the data after successful observering live data.

· Finally pass list of data to ShowAdapter using setDataIntoAdapter()

· In ShowAdapter.java class we have used fully data binding.

· Let’s see the xml file of ShowAdapter.

· We have provided ShowDetail.java model class to set the data inside the views using data binding.

· We have mapped ImageView from Image.java model class which is used in ShowDetail.java for GSON.

· @BindingAdapter annotation allows you to specify the method called to set a value, provide your own binding logic, and specify the type of the returned object by using adapters.

·We have used Glide library that fetch image from url.

we will consume shows detailed api which provide the details of particular shows.

Api = https://api.tvmaze.com/shows/8569

Let’s create GSON file for this api’s response:

Tip: How to create safe and fast GSON class.

· Hit the api in postman or browser and get the response then copy whole json.

· Search on google “convert json to gson” and choose any of them. I would suggest you to use always http://www.jsonschema2pojo.org/

· Now generate pojo class by clicking generating button and create new class.

We need to create ShowDetailActivity class to display the data.

· Create an xml file to bind the result.

Now have a look in our api implementation:

· We have already created interface i.e. Service.java.

· getShowById(): which provide the details of shows.

· Let’s consume this api in our repository class i.e. ShowsRepository.java

· We need to implement executeShowByIdApi() in our view model class i.e. ShowsViewModel.java

· Now we need to observe the live data from this view model in our activity class i.e. ShowDetailActivity.java

· We have mapped whole data into xml using data binding after observing live data from view model.

Let’s run the application to see our efforts:

· Loading all the show and click to any show and get the details.

Now Implement cast member api:

Api = https://api.tvmaze.com/shows/2831/cast

· We need to create get api method inside Service.java interface to consume cast api.

· This method will provide the array of Cast gson pojo.

· We need to map our json response into GSON pojo class.

· Let’s create Cast pojo class inside our project.

· We need to consume this api inside repository class

· Let’s see the code.

· Now we have to invoke this method inside our view model class.

· We need to provide live data to our view componets i.e. CastActivity.java from our view model class.

· We have to observe the live inside our activity.

· We have created CastActivity.java class.

· We need to add recyclerview inside our xml file to set the adater.

· We have observed live data from view model class now we need to bind the data into CastAdapter.java class.

· Let’s see the code below

· We have mapped list of Cast into our xml file.

· Finally our implementation is done.

Now let’s see the result below:

Thanks for reading this article. Be sure to like and comments to this article, if you found it helpful. It means a lot to me.

let me know if you want me to cover any other topic.

--

--