VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/how-to-get-data-from-api-using-retrofit-library-in-android/

⇱ How to GET Data From API using Retrofit Library in Android? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to GET Data From API using Retrofit Library in Android?

Last Updated : 23 Jul, 2025

A GET request is one of the HTTP request methods that can be used to retrieve data from a server. In an API context, a GET request is typically used to retrieve a specific resource or a collection of resources from a server. When a client makes a GET request to a server, it sends a request message that contains the URL of the desired resource along with any optional query parameters. The server then responds with a response message that contains the requested data, typically in the form of a JSON or XML document.

In this article we are going to see how we can GET data from an API using the Retrofit library, for this we have to follow some steps :

  1. Define a data model that represents the response data you expect to receive from the API endpoint.
  2. Define an interface that describes the API endpoints you want to access. This interface should include one or more methods that specify the HTTP method (e.g. GET), the endpoint URL, and any query parameters 
  3. Use Retrofit to create an implementation of your API interface. This implementation will handle making HTTP requests and parsing the response data.
  4. Call the desired method on the implementation to initiate the HTTP request. You can use the enqueue() method to execute the request asynchronously and receive the response data in a callback function.

Api Link:https://api.chucknorris.io/jokes/random
About the API: it is a free Jokes API. The Jokes are refreshed in every Call. It is a GET Request API Call.


Step by Step Implementation:

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio and select the language as Kotlin

👁 Retrofit_API
Directory Structure of Application


Step 2: Add the below dependency in your build.gradle file

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section. 

dependencies {
...
implementation ("com.squareup.retrofit2:retrofit:2.9.0")
implementation ("com.squareup.retrofit2:converter-gson:2.9.0")
}

After adding you need to sync your project.

Note: The Dependencies added will only work in Kotlin DSL Configuration Language not for Groovy DSL in case of Groovy DSL enclose the dependency in the single quotes(') and remove the round brackets.


Step 3: Adding permissions to the internet in the AndroidManifest.xml file

Navigate to the app > AndroidManifest.xml and add the below code to it. 

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

Step 4: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

activity_main.xml:


Step 5: Creating a Model Class for our jokes  

Navigate to the app > java > your app’s package name > Right-click on it > New > create a data class named as DataModel and add the below code to it. Comments are added inside the code to understand the code in more detail.

DataModel.kt:


Step 6: Creating an Interface class for our API

Navigate to the app > java > your app’s package name > Right-click on it > New > interface and name the file as ApiService and add the below code to it. Comments are added inside the code to understand the code in more detail.

ApiService.kt:


Step 7: Create a class for handling API Calls

Navigate to the app > java > your app’s package name > Right-click on it > New > class and name the file as ApiCall and add the below code to it. Comments are added inside the code to understand the code in more detail. In this call, we are going to make a Get request to our API using the Retrofit library.

ApiCall.kt:


Step 8: Working with the MainActivity.kt file

Go to the MainActivity.kt and follow the below code. Below is the code for the MainActivity.kt. Comments are added inside the code to understand the code in more detail. In this mainly we are going to call the ApiCall class getJokes() method.

MainActivity.kt:

Output:


Check out the github repo to get the entire code: Using Retrofit in the Kotlin Android Application

Comment
Article Tags:

Explore