Retrofit is one of the most used Android libraries for Networking which Google also recommends us to use in our application. Along with that, this guide will help you understand how to use the Retrofit module, inject it using the Dependency Injection technique (Hilt) in our MVVM Architecture and call the REST APIs and receive callbacks using RxJava all in Kotlin!

Implementation —

First Step —

We’ll first add the necessary retrofit dependencies along with RxJava and Gson support and OkHttp Interceptor—

//OkHTTP and Retrofit
def retrofit_version = "2.9.0"
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit_version"
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:4.4.1'

Second Step —

We’ll add Hilt support in our app so that we can inject the Retrofit module into our repository class. To read more about Hilt, I have written an in-depth article on Hilt and its implementation in Java — here. For Kotlin implementation, please follow the below steps —

a. Adding Hilt dependency —

//Hilt
implementation "com.google.dagger:hilt-android:2.35"
kapt 'com.google.dagger:hilt-android-compiler:2.35'

b. Adding Hilt Annotations —

We’ll be required to add few annotations. First, we will add one in the class which extends the Application class — ‘@HiltAndroidApp’. In each of our activity/fragments, we will add — ‘@AndroidEntryPoint’. In our view model, we’ll add the — ‘@HiltViewModel’. All set!

Now, we will create a Retrofit module which we will be able to inject into our repository —

Third Step —

To use Retrofit, we will need to define an interface where we can specify the endpoint, method, and the parameters we’ll be passing —

Here you will see that we have specified an annotation that indicates that it is a POST method call and the endpoint is attached to the APIConstant which we used while constructing the Retrofit Builder in the Hilt module. In the parameters of the function, we will pass the parameters we want to pass in the body annotated with — ‘@Field(“…”)’. The annotation — ‘@FormUrlEncoded’ helps us pass the parameters as Form Encoded in the body of the API request.

For the response, we have specified the model class as — ‘NewsAPIResponse’.

Fourth Step —

In the retrofit module, first, we will inject the ApiService of the Retrofit module using Hilt in our repository (constructor injection) —

We will also inject the NewsRepository in our ViewModel —

In the repository, we will consume the — ‘getNews()’ function we defined in the ApiService interface. And then we will use the getNews() function of our NewsRepository in the fragment/activity as —

Conclusion —

Tada! Now we are making network calls using Retrofit in our MVVM Architecture written in Kotlin with Data Binding and RxJava callbacks! Instead of RxJava, we can also make use of Kotlin Coroutines which is a concurrency design pattern for asynchronous programming.

MVVM Series –

Leave a Reply

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