Retrofit Android Example JSON Parser in Kotlin

Retrofit android is a type-safe HTTP client for Android and Java. Retrofit is a REST Client for Java, Android, and Kotlin by Square inc under Apache 2.0 license. With this library or module used the in-app app for server smooth connection, like sing in sing up or downloading-uploading data.

Retrofit android example Json parser in kotlin


Why Retrofit library? Because it will save your development time and also you can keep your code in developer friendly. Retrofit has given almost all the API’s to make a server call and to receive a response. Internally they also use GSON to do the parsing.


Let’s build an Example of Retrofit Android Example JSON Parser in Kotlin

In this example, we are using a simple example to connect your android application to the server using Retrofit 2 library. Let’s start building the application with simple and few steps.


Step 1. Create a new application, check this tutorial Build Your First Android App in Kotlin.
Step 2. In dependencies Gradle build.gradle (Module: app), 

Add GSON and retrofit 2 dependencies, GSON retrofit dependency is convertersResponseBody  to JSON object.

Retrofit requires at minimum Java 7 or Android 2.3., After adding the library just sync your project.

Step 3. For this example, we need an API

So we are using GitHub search API.

Here is response formate from API, data may change time to time depends on API.

Step 4. Create 2 models class

Because the json itself is an array of your items objects (Retrofit can convert between json arrays to java lists). For the second json, you have just an object and not an array. For this, you need to create another call with another model that maps to that json. Let’s assume you’ve named the model UsersList. Here’s how the call could look like:

1st UsersList model, Top level – List of Users

2nd Users model, second level  – Users objects

Step 4. Create API interface

Step 5. add following code in resource layout file main_activity.xml

Step 6. add following code in MainActivity kotlin class.

Step 7. add internet permissions in manifest.xml, for internet connection

Without Internet permission, Android application will raise an exception or error.

Step 8. Run the application, in the emulator or on your android device

Output screenshot Retrofit android example Json parser in kotlin

Retrofit android example Json parser in kotlin tutorial


Download Retrofit android Json parser example source code  :


Note : This example (Project) is developed in Android Studio 3.0.1 ,tested on Android 7.1.1 ( Android Nougat), compile SDK version API 26: Android 8.0 (Oreo)



Coding in Kotlin


By default, Retrofit can only deserialize HTTP bodies into OkHttp’s ResponseBody type and it can only accept its RequestBody type for @Body. You can converters can add to support other types. like

  • Gson: com.squareup.retrofit2:converter-gson
  • Simple XML: com.squareup.retrofit2:converter-simplexml


3 thoughts on “Retrofit Android Example JSON Parser in Kotlin

  1. Hi,
    Step 4 must be :
    import retrofit2.Call
    import retrofit2.http.GET

    interface Api {
    fun getUsers() : Call
    If you’re in Kotlin, right ?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.