A Beginner’s Guide to Retrofit in Android (Java)
<p>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".</p>
<p>Step 1: Add internet permission</p>
<p>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:</p>
<pre>
<uses-permission android:name="android.permission.INTERNET" /></pre>
<p>Step 2: Add the Retrofit library</p>
<p>To add the Retrofit library to your project, you’ll need to open the <code>build.gradle</code> file for your app module. In Android Studio, you can find it by expanding the <code>Gradle Scripts</code> folder.</p>
<p>Once you have the <code>build.gradle</code> file open, you’ll need to add the following dependencies to the <code>dependencies</code> block:</p>
<pre>
dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
}</pre>
<p>The first dependency, <code>implementation 'com.squareup.retrofit2:retrofit:2.9.0'</code>, adds the Retrofit library to your project. This library provides the core functionality for making HTTP requests and handling responses.</p>
<p>The second dependency, <code>implementation 'com.squareup.retrofit2:converter-gson:2.9.0'</code>, adds a converter for Gson to your project. This converter allows Retrofit to automatically convert JSON data into Java objects using the Gson library.</p>
<p>After adding these dependencies, make sure to sync your project with Gradle by clicking on the <code>Sync Now</code> button that appears in the top right corner of Android Studio.</p>
<p>Step 3: Add a POJO class</p>
<p>After adding the Retrofit and Gson dependencies to your project, the next step is to create a <code>models</code> directory and add a POJO (Plain Old Java Object) class named <code>Todo</code>. This class will represent the data model for a todo item that we’ll be fetching from the API.</p>
<p>Here’s how you can create the <code>models</code> directory and add the <code>Todo</code> class:</p>
<ol>
<li>In Android Studio, right-click on the <strong>com.exmple.yourappname </strong>(In my case it is com.exmple.retrofitexample) directory in your app module and select <code>New</code> > <code>Package</code>.</li>
<li>Enter <code>models</code> as the package name and click <code>OK</code>.</li>
<li>Right-click on the newly created <code>models</code> package and select <code>New</code> > <code>Java Class</code>.</li>
<li>Enter <code>Todo</code> as the class name and click <code>OK</code>.</li>
</ol>
<p>Now you should have a new <code>Todo.java</code> file in the <code>models</code> package. This file will contain the definition of the <code>Todo</code> class.</p>
<p>The <code>Todo</code> 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.</p>
<p><a href="https://medium.com/@nikhil725051/a-beginners-guide-to-retrofit-in-android-java-d95bcc257fdc">Visit Now</a></p>