Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
Introduction to Retrofit in Android
Next article icon

Introduction to Retrofit in Android

Last Updated : 18 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  • How to Post Data to API using Retrofit in Android?
  • How to Update Data in API using Retrofit in Android?

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:

ResponseData.java
package org.geeksforgeeks.demo;  import com.google.gson.annotations.SerializedName;  public class ResponseData {          @SerializedName("body")     private String body;          @SerializedName("id")     private int id;          @SerializedName("title")     private String title;          @SerializedName("userId")     private int userId;      public ResponseData(String body, int id, String title, int userId) {         this.body = body;         this.id = id;         this.title = title;         this.userId = userId;     }      public String getBody() {         return body;     }      public void setBody(String body) {         this.body = body;     }      public int getId() {         return id;     }      public void setId(int id) {         this.id = id;     }      public String getTitle() {         return title;     }      public void setTitle(String title) {         this.title = title;     }      public int getUserId() {         return userId;     }      public void setUserId(int userId) {         this.userId = userId;     } } 
ResponseData.kt
package org.geeksforgeeks.demo  data class ResponseData(     val body: String,     val id: Int,     val title: String,     val userId: Int ) 


Step 4: Create a Retrofit Object/Instance

Create an retrofit instance to define API base url.

RetrofitInstance Class:

RetrofitInstance.java
package org.geeksforgeeks.demo;  import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory;  public class RetrofitInstance {     private static final String BASE_URL = "https://jsonplaceholder.typicode.com";     private static Retrofit retrofit;      public static Retrofit getRetrofitInstance() {         if (retrofit == null) {             retrofit = new Retrofit.Builder()                     .baseUrl(BASE_URL)                     .addConverterFactory(GsonConverterFactory.create())                     .build();         }         return retrofit;     }      public static ApiInterface getApiInterface() {         return getRetrofitInstance().create(ApiInterface.class);     } } 
RetrofitInstance.kt
package org.geeksforgeeks.demo  import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory  object RetrofitInstance {     private val retrofit by lazy {         Retrofit.Builder().baseUrl("https://jsonplaceholder.typicode.com")             .addConverterFactory(GsonConverterFactory.create())             .build()     }      val apiInterface by lazy {         retrofit.create(ApiInterface::class.java)     } } 


Step 5: Create a API interface

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

ApiInterface Class:

ApiInterface.java
package org.geeksforgeeks.demo;  import retrofit2.Call; import retrofit2.http.GET;  public interface ApiInterface {     @GET("/posts/1")     Call<ResponseData> getData(); } 
ApiInterface.kt
package org.geeksforgeeks.demo  import retrofit2.Call import retrofit2.http.GET  interface ApiInterface {     @GET("/posts/1")     fun getData(): Call<ResponseData> } 


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:

activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".MainActivity">       <TextView         android:id="@+id/title_textView"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_marginStart="32dp"         android:layout_marginEnd="32dp"         android:text="Title"         android:textSize="24sp"         android:textStyle="bold"         app:layout_constraintBottom_toTopOf="@+id/body_textView"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_chainStyle="packed" />      <TextView         android:id="@+id/body_textView"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_marginStart="32dp"         android:layout_marginTop="32dp"         android:layout_marginEnd="32dp"         android:text="Body"         android:textSize="16sp"         android:textStyle="italic"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@+id/title_textView" />      <Button         android:id="@+id/getData_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginTop="32dp"         android:text="Get Data"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@+id/body_textView" /> </androidx.constraintlayout.widget.ConstraintLayout> 

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:

MainActivity.java
package org.geeksforgeeks.demo;  import android.app.ProgressDialog; import android.os.Bundle; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response;  public class MainActivity extends AppCompatActivity {      private TextView titleTextView;     private TextView bodyTextView;     private Button getDataButton;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          titleTextView = findViewById(R.id.title_textView);         bodyTextView = findViewById(R.id.body_textView);         getDataButton = findViewById(R.id.getData_button);          getDataButton.setOnClickListener(view -> getData());     }      private void getData() {         ProgressDialog progressDialog = new ProgressDialog(this);         progressDialog.setMessage("Please wait");         progressDialog.show();          RetrofitInstance.getApiInterface().getData().enqueue(new Callback<ResponseData>() {             @Override             public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {                 ResponseData responseData = response.body();                 if (responseData != null) {                     titleTextView.setText(responseData.getTitle());                     bodyTextView.setText(responseData.getBody());                 }                 progressDialog.dismiss();             }              @Override             public void onFailure(Call<ResponseData> call, Throwable t) {                 Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();                 progressDialog.dismiss();             }         });     } } 
MainActivity.kt
package org.geeksforgeeks.demo  import android.app.ProgressDialog import android.os.Bundle import android.widget.Button import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import retrofit2.Call import retrofit2.Callback import retrofit2.Response  class MainActivity : AppCompatActivity() {      private lateinit var titleTextView: TextView     private lateinit var bodyTextView: TextView     private lateinit var getDataButton: Button      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          titleTextView = findViewById(R.id.title_textView)         bodyTextView = findViewById(R.id.body_textView)         getDataButton = findViewById(R.id.getData_button)          getDataButton.setOnClickListener {             getData()         }     }      private fun getData() {          val progressDialog = ProgressDialog(this)         progressDialog.setMessage("Please wait")         progressDialog.show()          RetrofitInstance.apiInterface.getData().enqueue(object : Callback<ResponseData?> {             override fun onResponse(call: Call<ResponseData?>, response: Response<ResponseData?>) {                 val responseData = response.body()                 titleTextView.text = responseData?.title                 bodyTextView.text = responseData?.body                 progressDialog.dismiss()             }              override fun onFailure(call: Call<ResponseData?>, t: Throwable) {                 Toast.makeText(this@MainActivity, t.localizedMessage, Toast.LENGTH_SHORT).show()                 progressDialog.dismiss()             }         })     } } 

Output:


Next Article
Introduction to Retrofit in Android

A

Ashutosh Bhushan Srivastava
Improve
Article Tags :
  • GBlog
  • Android
  • Kotlin Android
  • Android Retrofit Library
  • Java-Android

Similar Reads

    Introduction to Android Jetpack
    The Android support libraries are used in almost all android applications to overcome the compatibility issues across different Android OS versions and devices. These libraries also facilitate users to add various kinds of updated widgets in the application. Over time, these libraries are updated ac
    6 min read
    Retrofit with Kotlin Coroutine in Android
    Retrofit is a type-safe http client which is used to retrieve, update and delete the data from web services. Nowadays retrofit library is popular among the developers to use the API key. The Kotlin team defines coroutines as “lightweight threads”. They are sort of tasks that the actual threads can e
    3 min read
    Introduction to Android Development
    Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow
    5 min read
    Networking and API Integration in Android
    Networking is a great way to connect your application with the world. Yes, before going straight into our topic, let's first try to find its necessity in your application. Networking is an essential aspect of modern applications, allowing them to connect with servers, databases, APIs, and other appl
    9 min read
    How to Post Data to API using Retrofit in Android?
    We have seen reading data from API in our Android app in Android Studio. For reading data from API, we use GET request to read our data which is in JSON format. In this article, we will take a look at adding data to REST API in our Android App in Android Studio. What we are going to build in this ar
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences