VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/how-to-post-data-to-api-using-retrofit-in-android-using-jetpack-compose/

⇱ How to Post Data to API using Retrofit in Android using Jetpack Compose? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Post Data to API using Retrofit in Android using Jetpack Compose?

Last Updated : 23 Jul, 2025

APIs are used within Android Applications to interact with databases to perform various CRUD operations on data within the database such as adding new data, reading the existing data, and updating and deleting existing data. In this article, we will take a look at How to Post Data to API using Retrofit in Android using Jetpack Compose.

Note: If you are seeking Java code for Jetpack Compose, please note that Jetpack Compose is only available in Kotlin. It uses features such as coroutines, and the handling of @Composable annotations is handled by a Kotlin compiler. There is no method for Java to access these. Therefore, you cannot use Jetpack Compose if your project does not support Kotlin.

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in the Android Studio, please refer to How to Create a new Project in Android Studio with Jetpack Compose.

Note: Select Kotlin as the programming language.

Step 2: Add the below dependency to your build.gradle File

Navigate to the Gradle Scripts > build.gradle.kts (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.5.0")
}

After adding this dependency sync your project.


Step 3: Adding Internet Permissions 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: Creating a Model Class for Storing Our Data  

Navigate to the app > kotlin+java > {package-name} > Right-click > New > Kotlin Class/File and name it 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 5: Creating an Interface Class for Our API Call

Navigate to the app > kotlin+java > {package-name} > Right-click > New > Kotlin Class/File select it as Interface and name the file as RetrofitAPI and add below code to it. Comments are added inside the code to understand the code in more detail.

RetrofitAPI.kt:


Step 6: Working with the MainActivity.java file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

MainActivity.kt:

Output:


Comment

Explore