How to make an API request in an Android app using Kotlin

Oscar de la Hera Gomez
Three flowers that represent Kotlin, Android and Android Studio side by side. Beneath them sits the text “API Requests.”

A step by step guide on making API requests (GET, POST, PUT, PATCH, DELETE) and converting the JSON into a readable output.

The following tutorial builds on our Open Source Kotlin project which can be downloaded using the link below, and aims to walk you through how to prepare your project for making API calls, through the examples provided by Google Volley's library.

We recommend that you checkout the main branch and carry out the steps described below. The changes that took place can be found on the tutorial/data/setup-api-requests branch.

git clone git@github.com:delasign/kotlin-android-starter-project.git

Step One: Add the Internet Permissions

A screenshot of Android Studio showing the AndroidManifest.xml file. Highlighted is where we have added the permissions - this is above the application section of the manifest.

In the projects AndroidManifest.xml file, add the following permission:

 <uses-permission android:name="android.permission.INTERNET" />

Step Two: Add the Dependencies

A screenshot of Android Studio showing the app level build.gradle.kts file. We have highlighted the dependencies that are described below.

In the App Level build.gradle.kts file, add the following dependencies.

implementation("com.android.volley:volley:1.2.1")

implementation("com.google.code.gson:gson:2.8.9")

Please note that versions displayed above may not be the latest versions, please update the code above to the latest version in your Android Studio project.

Step Three: Sync the Gradles

A screenshot of Android Studio showing a gradle file that has recently been modified. Highlighted is the "Sync Now" button that appears on the right of the prompt at the top of the Gradle file. Click this button to sync the gradle files with the project.

Press Sync Now in the bar above the Gradle to update the project dependencies.

Step Four: Create the GSON request class

A screenshot of AndroidStudio showing the GSONRequest.kt class. Code is available below.

In the project, create a new file called GsonRequest.kt and paste in the code below.

Step Five: Create the Request Response class

A screenshot of Android Studio showing a sample response data class.

In the project, create a new file which is named after the data class of the API request's response.

Step Six: Create the Error class

A screenshot of Android Studio showing a sample error data class.

In the project, create a new file which is named after the data class of the API request's error.

Please note that in the case of the API which we used to test our code, the response and error are identical.

Step Seven: Add a Request Queue

A screenshot of Android Studio showing the DataCoordinator. Highlighted is the declaration of the request queue as well as where we have initialized it.

Wherever you wish to make API calls, declare a RequestQueue and initialize it.

We suggest that you do this in a manager such as the DataCoordinator, which we created as part of our DataStore tutorial.

Step Eight: Setup the API call

A screenshot of Android Studio showing a sample function that makes an API call. Code available below.

Create a function to handle your API call and all the associated details.

We recommend that you do this in a manager such as the DataCoordinator and include callbacks, with data if needed, for the relevant cases.

Step Nine: Make the API call

A screenshot of Android Studio showing that we have called the function that makes the API request.

Call the function created in Step Eight, wherever you wish to make the API call in your project.

Step Ten: Test

A screenshot of Android Studio with the Logs showing that the request was successful.

Run the app to check that the API goes through successfully.

Step Eleven: Test an Error

A screenshot of Android Studio with the Logs showing that the request failed.

Make a modification to the function in Step Eight (Such as remove the JSON Payload) and test that you can receive errors correctly.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details