Skip to main content

How to send POST requests using Retrofit in Kotlin Android

How to send POST requests using Retrofit in Kotlin Android.

How to Send POST Requests using Retrofit in Kotlin Android

In this tutorial, we will learn how to send POST requests using Retrofit in Kotlin Android. Retrofit is a popular HTTP client library for Android that simplifies the process of making network requests.

Step 1: Add Retrofit to your Project

To use Retrofit in your project, you need to add the Retrofit dependency to your build.gradle file. Open your project's build.gradle file and add the following line to the dependencies section:

implementation 'com.squareup.retrofit2:retrofit:2.9.0'

Sync your project after adding the dependency.

Step 2: Create a Retrofit Instance

To send POST requests, we need to create an instance of the Retrofit class. Create a new Kotlin file called ApiClient.kt and add the following code:

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object ApiClient {
private const val BASE_URL = "https://api.example.com" // Replace with your API base URL

private val retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}

val apiService: ApiService by lazy {
retrofit.create(ApiService::class.java)
}
}

Make sure to replace https://api.example.com with your actual API base URL.

Step 3: Define the API Service Interface

Next, we need to define the API endpoints in an interface. Create a new Kotlin file called ApiService.kt and add the following code:

import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.POST

interface ApiService {
@POST("/api/endpoint")
fun postData(@Body requestBody: RequestBody): Call<ResponseData>
}

Replace /api/endpoint with the actual endpoint of your API. Also, replace RequestBody with the actual request body model class and ResponseData with the actual response model class.

Step 4: Create Request and Response Model Classes

Create Kotlin data classes for your request and response models. For example:

data class RequestBody(val name: String, val email: String)

data class ResponseData(val success: Boolean, val message: String)

Replace RequestBody and ResponseData with your actual model classes.

Step 5: Send POST Request

To send a POST request, we first need to create an instance of the request model class. For example:

val request = RequestBody("John Doe", "[email protected]")

Then, we can use the apiService instance from ApiClient to send the POST request. Add the following code to your activity or fragment:

val call = ApiClient.apiService.postData(request)
call.enqueue(object : Callback<ResponseData> {
override fun onResponse(call: Call<ResponseData>, response: Response<ResponseData>) {
if (response.isSuccessful) {
val responseData = response.body()
// Handle successful response
} else {
// Handle error response
}
}

override fun onFailure(call: Call<ResponseData>, t: Throwable) {
// Handle network error
}
})

Make sure to handle the success and error responses accordingly.

That's it! You have now learned how to send POST requests using Retrofit in Kotlin Android. Retrofit simplifies the process of making network requests and provides a clean and efficient way to communicate with APIs.

Remember to replace the placeholder URLs, model classes, and handle the responses according to your API's requirements.