Design Converter
Education
Last updated on Sep 15, 2023
Last updated on Jul 8, 2021
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.
1 dependencies { 2 // retrofit 3 compile "com.squareup.retrofit2:retrofit:2.3.0" 4 compile "com.squareup.retrofit2:adapter-rxjava2:2.3.0" 5 compile "com.squareup.retrofit2:converter-gson:2.3.0" 6 7 // rxandroid 8 compile "io.reactivex.rxjava2:rxandroid:2.0.1" 9 } 10
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:
1 // exclude Retrofit’s OkHttp dependency module and define your own // module 2 compile ("com.squareup.retrofit2:retrofit:2.1.0") { 3 import exclude module: 'okhttp' } 4 compile "com.google.code.gson:gson:2.6.2" 5 compile "com.squareup.retrofit2:converter-gson:2.1.0" 6 compile "com.squareup.okhttp3:logging-interceptor:3.4.1" 7 compile "com.squareup.okhttp3:okhttps:3.4.1" 8
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.
1 // Data class in Kotlin 2 data class User( 3 val login: String, 4 val id: Long, 5 val url: String, 6 val html_url: String, 7 val followers_url: String, 8 val following_url: String, 9 val starred_url: String, 10 val gists_url: String, 11 val type: String, 12 val score: Int 13 ) 14
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.
1 // Data class in Kotlin with final search response class 2 data class User( 3 val login: String, 4 val id: Long, 5 val url: String, 6 val html_url: String, 7 val followers_url: String, 8 val following_url: String, 9 val starred_url: String, 10 val gists_url: String, 11 val type: String, 12 val score: Int 13 ) 14 data class Result (val total_count: Int, val incomplete_results: Boolean, val items: List
The following step is to design an API interface that will be used to send requests and receive responses via retrofit.
GithubApiService.kt
1 interface GithubApiService { 2 3 @GET("search/users") 4 fun search(@Query("q") query: String, 5 @Query("page") page: Int, 6 @Query("per_page") perPage: Int): Observable
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.
1 object SearchRepositoryProvider { 2 fun provideSearchRepository(): SearchRepository { 3 return SearchRepository() 4 } 5 } 6 7
The above singleton object can be used as,
1 val repository = SearchRepositoryProvider.provideSearchRepository(); 2 3 4
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:
1 class SearchRepository(val apiService: GithubApiService) { 2 fun searchUsers(location: String, language: String): Observable
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:
1 val repository = SearchRepositoryProvider.provideSearchRepository() 2 repository.searchUsers("Lagos", "Java") 3 .observeOn(AndroidSchedulers.mainThread()) 4 .subscribeOn(Schedulers.io()) 5 .subscribe ({ 6 result -> 7 Log.d("Result", "There are ${result.items.size} Java developers in Lagos") 8 }, { error -> 9 error.printStackTrace() 10 }) 11
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!!
https://www.segunfamisa.com/posts/using-retrofit-on-android-with-kotlin
https://www.journaldev.com/13639/retrofit-android-example-tutorial
https://www.youtube.com/watch?v=seuRVttjQdM
https://jakewharton.com/making-retrofit-work-for-you-ohio/
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.