VOOZH about

URL: https://www.geeksforgeeks.org/android/introduction-retofit-2-android-set-1/

⇱ Introduction to Retrofit in Android - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Introduction to Retrofit in Android

Last Updated : 18 Feb, 2025

Retrofit is a type-safe HTTP client for Android, developed by Square. It simplifies network operations by allowing developers to define REST API interactions using Java/Kotlin interfaces. It supports various request methods like GET, POST, PUT, DELETE, and PATCH while enabling seamless integration with JSON parsing libraries like Gson and Moshi.

Why Use Retrofit?

The advantages that Retrofit has over traditional network handling methods like HttpURLConnection and Volley are:

  1. Provides a clean and simple way to define API requests.
  2. Converts API responses directly into Java/Kotlin objects, making it type safe.
  3. Supports both asynchronous calls (using enqueue) and synchronous calls (using execute).
  4. Allows adding custom headers, logging, and authentication easily.
  5. Can be extended with converter, thus highly scalable.

Step by Step Implementation:

In this article, we will be implementing the GET request method. To know more about the other request methods refer to

Step 1: Add necessary dependencies

For using Retrofit in our Android project firstly we have to add dependency in the gradle file. For adding dependency open app/build.gradle file in your Android project and add the following lines inside it. Add these lines inside dependencies{}

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

Step 2: Provide internet permission

We should now add Internet Permission inside Manifest file. Open the AndroidManifest.xml file and add the following line. 

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

Step 3: Create a model class for data response from API

For retrieving data from the server using retrofit 2 we require a model class. We are going to make a model class to retrieve data from the server. Set the name of the class ResponseData.

👁 directory-structure-retrofit

ResponseData Class:


Step 4: Create a Retrofit Object/Instance

Create an retrofit instance to define API base url.

RetrofitInstance Class:


Step 5: Create a API interface

Create an interface to define API endpoints and their request types.

ApiInterface Class:


Step 6: Working with activity_main.xml

Navigate to app > res > layout > activity_main.xml and make the below changes. We will be adding 2 textviews and a button to fetch the data.

activity_main.xml:

Design UI:

👁 design-ui-retrofit

Step 7: Working with MainActivity file

In this file, we will call the Retrofit Instance to get the data and bind it to the view. Navigate to app > kotlin+java > {package-name} > MainActivity.java/MainActivity.kt and make the necessary changes given below.

MainActivity File:

Output:

Comment

Explore