In the world of Android app development, making network requests to fetch data from APIs is a common task. Retrofit is a powerful and popular library that simplifies this process by providing a clean and efficient way to make API calls. In this tutorial, we’ll walk through how to use Retrofit to fetch a list of todos from a mock API and display their titles in a ListView using an ArrayAdapter. For this tutorial, we’ll use the mock API “https://jsonplaceholder.typicode.com/todos".
Step 1: Add internet permission
Before your Android app can communicate with remote servers or APIs, you need to grant it the necessary permissions. To enable internet access for your app, you’ll need to add the “INTERNET” permission to your app’s AndroidManifest.xml file. Add the following line inside the <manifest> tag:
<uses-permission android:name="android.permission.INTERNET" />
Step 2: Add the Retrofit library
To add the Retrofit library to your project, you’ll need to open the build.gradle file for your app module. In Android Studio, you can find it by expanding the Gradle Scripts folder.
Once you have the build.gradle file open, you’ll need to add the following dependencies to the dependencies block:
dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
}
The first dependency, implementation 'com.squareup.retrofit2:retrofit:2.9.0', adds the Retrofit library to your project. This library provides the core functionality for making HTTP requests and handling responses.
The second dependency, implementation 'com.squareup.retrofit2:converter-gson:2.9.0', adds a converter for Gson to your project. This converter allows Retrofit to automatically convert JSON data into Java objects using the Gson library.
After adding these dependencies, make sure to sync your project with Gradle by clicking on the Sync Now button that appears in the top right corner of Android Studio.
Step 3: Add a POJO class
After adding the Retrofit and Gson dependencies to your project, the next step is to create a models directory and add a POJO (Plain Old Java Object) class named Todo. This class will represent the data model for a todo item that we’ll be fetching from the API.
Here’s how you can create the models directory and add the Todo class:
- In Android Studio, right-click on the com.exmple.yourappname (In my case it is com.exmple.retrofitexample) directory in your app module and select
New>Package. - Enter
modelsas the package name and clickOK. - Right-click on the newly created
modelspackage and selectNew>Java Class. - Enter
Todoas the class name and clickOK.
Now you should have a new Todo.java file in the models package. This file will contain the definition of the Todo class.
The Todo class is needed for Retrofit because it defines the data model for the todo items that we’ll be fetching from the API. Retrofit uses this class to automatically convert the JSON data returned by the API into Java objects that we can use in our app.