Skip to main content

How to send GET requests using Retrofit in Kotlin Android

How to send GET requests using Retrofit in Kotlin Android.

Here's a step-by-step tutorial on how to send GET requests using Retrofit in Kotlin Android:

Step 1: Add dependencies

To get started, you need to add the necessary dependencies to your project. Open your project's build.gradle file and add the following lines to the dependencies block:

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

Step 2: Create a Retrofit instance

Next, you need to create a Retrofit instance. This instance will serve as the entry point for all your network requests. Create a new Kotlin file and add the following code:

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

val BASE_URL = "https://api.example.com/"

val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()

Make sure to replace BASE_URL with the base URL of the API you're working with.

Step 3: Define an API interface

Now, you need to define an interface that describes the API endpoints you'll be calling. Create a new Kotlin file and add the following code:

import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query

interface ApiService {
@GET("posts")
fun getPosts(@Query("userId") userId: Int): Call<List<Post>>
}

In this example, we're defining a getPosts function that takes a userId parameter and returns a Call object that will hold the response data.

Step 4: Make the API call

To make the GET request, you'll need to create an instance of the API service and call the desired endpoint function. In your activity or fragment, add the following code:

val apiService = retrofit.create(ApiService::class.java)

val call = apiService.getPosts(1)
call.enqueue(object : Callback<List<Post>> {
override fun onResponse(call: Call<List<Post>>, response: Response<List<Post>>) {
if (response.isSuccessful) {
val posts = response.body()
// Do something with the posts
} else {
// Handle error
}
}

override fun onFailure(call: Call<List<Post>>, t: Throwable) {
// Handle failure
}
})

This code creates an instance of the API service using the Retrofit instance we created earlier. Then, it calls the getPosts function with a userId of 1. Finally, it enqueues the request and provides callbacks for handling the response.

Step 5: Handle the response

Inside the onResponse callback, you can access the response data and handle it accordingly. In this example, we're extracting the list of Post objects from the response body. You can perform any desired data manipulation or display logic here.

Step 6: Handle errors and failures

Inside the onFailure callback, you can handle any network failures that occur during the request. This is where you can show error messages to the user or perform any necessary error handling.

That's it! You now know how to send GET requests using Retrofit in Kotlin Android. Remember to handle errors, failures, and edge cases appropriately in your own code. Retrofit offers many other features and options for customization, so make sure to refer to the official documentation for more information.