Traditionally, performing networking tasks in Android was a nightmare. Communicating with the server by placing network calls involves lots of effort as developers often preferred to make use of the main thread to perform networking tasks.
It causes the Android application to slow down or sometimes freeze affecting user experience.
However today they have a whole range of REST client libraries that speed up app development by reducing the app networking efforts.
In 2013, Google developed Volly, which can access REST APIs. Still, there was room for improvement thus Square introduced Retrofit- A powerful library to interact with REST API.
Retrofit 2 is the type-safe HTTP client, built by Square Inc for Java and Kotlin applications that simplify authenticating and interacting with REST APIs and sending network requests with OkHttp.
Retrofit is a very powerful library that can make the most problematic API easy to use. Retrofit 2 makes networking easier by offering an abstraction layer over the HTTP calls and automatically generating complex code files.
It is designed to simplify endpoint declaration i.e how requests are created and sent to the server and how responses are read and parsed. It naturally serialises and deserialises the JSON reactions and directly returns POJO (Plain Old Java Objects).
Other than the above it is used to perform the following tasks:
The only disadvantage with Retrofit, it doesn’t provide support for image loading from the server. In that case, other libraries such as Glide, Picasso, or Fresco can be used.
Here will use Retrofit, Kotlin, and RxJava to demonstrate how it simplifies REST API interaction in Android applications. We will make use of the Github API and a list of Java developers in Lagos.Before you begin implementation, make sure you have the Kotlin development environment ready. Kotlin is supported natively in Android Studio 3.0 and later.
To use Retrofit in your application, you need to add some dependencies to your app module build.gradle file.
In the above code snippet, the first dependency is the Retrofit dependency. The second one is for the RxJava2 adapter to make calls reactive through the set of interfaces and methods. The third is the GSON converter that handles data serialisation and deserialisation of request and response bodies to and from JSON format. The final dependency is RxAndroid, it is an extension to RxJava2 it provides a scheduler for running code in the Android application's main thread.
Retrofit 2 already includes the OkHttp dependency. If you wish to use a separate OkHttp dependency, exclude it from Retrofit2 as:
The logging interceptor generates a log string for the entire response. There are also other converters to parse Json to the required type, some of which are,
Add the permission to access the internet in the AndroidManifest.xml file.
The next step is to create data classes with Plain Old Java Object that will represent the response of API calls. In Kotlin, data classes are designed for the classes that hold the data.
To define a data class, we simply add a data keyword before the class name. Further, the Kotlin compiler assists in automatically implementing methods on a class, resulting in even shorter code.
In Kotlin, we can create a single search response file like getSearchResponse.kt. The final search response class will contain all the related data classes.
The following step is to design an API interface that will be used to send requests and receive responses via retrofit.
The interface and the factory class can be used as,
We didn’t have to use the name of the companion object to reference the method; we can simply use the class name as a qualifier.
Singleton objects in Kotlin
The singleton object is one that only needs to be created once and can be used everywhere.
The above singleton object can be used as,
If you want to write a function or member of a class that can be called without having an instance of the class, you can do so using a companion object inside the class in Kotlin.
So, by declaring the companion object, you can access the members of a class by name only. You can create a companion object by adding the Companion keyword in front of the object declaration.
Create a simple repository that handles calling the GithubApiServie and builds the query string. Create a method in the repository that will allow you to build the string by taking location and language as parameters.
The search repository will look like this:
In the preceding code snippet, we used the string template feature to create a query string. The string template starts with $ sign.
Now create the request and retrieve the API response using RxJava. In Kotlin code, it looks like this:
Through this implementation, you can understand how to use Retrofit with Kotlin.
As of now you have a clear understanding of retrofit and its implementation. So it's time to move and improve your team productivity with DhiWise- An ultimate platform for developers to build enterprise-grade apps 2X faster with minimum resources.
The platform harnesses the power of automation to increase developers’ productivity while offering full flexibility of customization.
Yes, you hear it right!
With DhiWise Android Builder for Kotlin(Language recommended by Google for Android app development), along with the supported libraries including Retrofit, developers can efficiently generate clean code for their app in a few steps.
So what are you waiting for? Try DhiWise Today!!