<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dhananjay Bagul</title>
    <description>The latest articles on DEV Community by Dhananjay Bagul (@dhananjay_bagul).</description>
    <link>https://dev.to/dhananjay_bagul</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4174492%2Fdc9fe432-21fa-4d9d-9b4b-69bdaa0880d4.webp</url>
      <title>DEV Community: Dhananjay Bagul</title>
      <link>https://dev.to/dhananjay_bagul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dhananjay_bagul"/>
    <language>en</language>
    <item>
      <title>Connecting an Android App to a REST API Using Retrofit in Java</title>
      <dc:creator>Dhananjay Bagul</dc:creator>
      <pubDate>Sat, 10 Oct 2026 21:13:03 +0000</pubDate>
      <link>https://dev.to/dhananjay_bagul/connecting-an-android-app-to-a-rest-api-using-retrofit-in-java-3j6g</link>
      <guid>https://dev.to/dhananjay_bagul/connecting-an-android-app-to-a-rest-api-using-retrofit-in-java-3j6g</guid>
      <description>&lt;p&gt;When building an Android application, we often need to communicate with a backend server to fetch or send data. For example, an HRMS app may need to display employees, a shopping app may need to load products, or a booking app may need to retrieve reservations.&lt;/p&gt;

&lt;p&gt;One popular way to make these network requests in Android is Retrofit.&lt;/p&gt;

&lt;p&gt;In this article, we will learn how to connect an Android application to a REST API using Java and Retrofit, step by step.&lt;/p&gt;

&lt;p&gt;What Is Retrofit?&lt;/p&gt;

&lt;p&gt;Retrofit is a type-safe HTTP client for Android and Java. It helps us make API requests without writing all the networking code manually.&lt;/p&gt;

&lt;p&gt;With Retrofit, we can:&lt;/p&gt;

&lt;p&gt;Send GET requests to fetch data.&lt;/p&gt;

&lt;p&gt;Send POST requests to submit data.&lt;/p&gt;

&lt;p&gt;Convert JSON responses into Java objects using a converter.&lt;/p&gt;

&lt;p&gt;Handle successful responses and network errors.&lt;/p&gt;

&lt;p&gt;Keep API-related code organized in a separate interface.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add Retrofit Dependencies&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Open your app-level build.gradle file and add these dependencies inside dependencies:&lt;/p&gt;

&lt;p&gt;implementation 'com.squareup.retrofit2:retrofit:2.11.0'&lt;br&gt;
implementation 'com.squareup.retrofit2:converter-gson:2.11.0'&lt;/p&gt;

&lt;p&gt;Sync the project after adding them. If your project uses build.gradle.kts, use the Kotlin DSL syntax for dependencies.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add Internet Permission&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Open AndroidManifest.xml and add this permission above the  tag:&lt;/p&gt;



&lt;p&gt;This permission allows your application to access the internet.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a Java Model Class&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Suppose our backend returns employee information in JSON format:&lt;/p&gt;

&lt;p&gt;{&lt;br&gt;
  "id": 1,&lt;br&gt;
  "name": "Rahul",&lt;br&gt;
  "email": "&lt;a href="mailto:rahul@example.com"&gt;rahul@example.com&lt;/a&gt;"&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Create a Java class named Employee.java:&lt;/p&gt;

&lt;p&gt;public class Employee {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private int id;
private String name;
private String email;

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public String getEmail() {
    return email;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Gson converts the JSON response into an Employee object when the JSON field names match the Java fields.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create the API Interface&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Create a file named ApiService.java:&lt;/p&gt;

&lt;p&gt;import java.util.List;&lt;br&gt;
import retrofit2.Call;&lt;br&gt;
import retrofit2.http.GET;&lt;/p&gt;

&lt;p&gt;public interface ApiService {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@GET("api/employees")
Call&amp;lt;List&amp;lt;Employee&amp;gt;&amp;gt; getEmployees();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;The &lt;a class="mentioned-user" href="https://dev.to/get"&gt;@get&lt;/a&gt; annotation tells Retrofit to send a GET request to the api/employees endpoint.&lt;/p&gt;

&lt;p&gt;Replace this endpoint with the path exposed by your own backend API.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Configure Retrofit&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Create a class named ApiClient.java:&lt;/p&gt;

&lt;p&gt;import retrofit2.Retrofit;&lt;br&gt;
import retrofit2.converter.gson.GsonConverterFactory;&lt;/p&gt;

&lt;p&gt;public class ApiClient {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private static final String BASE_URL =
        "https://your-domain.com/";

private static Retrofit retrofit;

public static ApiService getApiService() {

    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(
                        GsonConverterFactory.create()
                )
                .build();
    }

    return retrofit.create(ApiService.class);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Replace &lt;a href="https://your-domain.com/" rel="noopener noreferrer"&gt;https://your-domain.com/&lt;/a&gt; with your backend's base URL. Keep the trailing slash because Retrofit requires the base URL to end with /.&lt;/p&gt;

&lt;p&gt;For local testing with the Android emulator, a backend running on your development computer can often be reached using &lt;a href="http://10.0.2.2:8080/" rel="noopener noreferrer"&gt;http://10.0.2.2:8080/&lt;/a&gt;. Configure local HTTP access appropriately for development, or use HTTPS.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make an API Request from an Activity&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, call the API from your Activity:&lt;/p&gt;

&lt;p&gt;import android.widget.Toast;&lt;br&gt;
import java.util.List;&lt;/p&gt;

&lt;p&gt;import retrofit2.Call;&lt;br&gt;
import retrofit2.Callback;&lt;br&gt;
import retrofit2.Response;&lt;/p&gt;

&lt;p&gt;ApiService apiService = ApiClient.getApiService();&lt;/p&gt;

&lt;p&gt;apiService.getEmployees().enqueue(&lt;br&gt;
        new Callback&amp;gt;() {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Override
public void onResponse(
        Call&amp;lt;List&amp;lt;Employee&amp;gt;&amp;gt; call,
        Response&amp;lt;List&amp;lt;Employee&amp;gt;&amp;gt; response) {

    if (response.isSuccessful()
            &amp;amp;&amp;amp; response.body() != null) {

        List&amp;lt;Employee&amp;gt; employees = response.body();

        // Pass the employee list to your RecyclerView adapter.

    } else {
        Toast.makeText(
                MainActivity.this,
                "Unable to load employees",
                Toast.LENGTH_SHORT
        ).show();
    }
}

@Override
public void onFailure(
        Call&amp;lt;List&amp;lt;Employee&amp;gt;&amp;gt; call,
        Throwable t) {

    Toast.makeText(
            MainActivity.this,
            "Network error: " + t.getMessage(),
            Toast.LENGTH_SHORT
    ).show();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;This example assumes the request is made inside MainActivity. In a real project, keep the networking logic in an appropriate class and update your UI through your chosen architecture.&lt;/p&gt;

&lt;p&gt;The important part is enqueue(). It executes the request asynchronously, so the network call does not block the main UI thread.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Connect It to a Spring Boot Backend&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If your backend is built with Spring Boot, it might expose an endpoint like this:&lt;/p&gt;

&lt;p&gt;@RestController&lt;br&gt;
@RequestMapping("/api/employees")&lt;br&gt;
public class EmployeeController {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@GetMapping
public List&amp;lt;Employee&amp;gt; getEmployees() {
    return employeeService.getAllEmployees();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;The Android endpoint and the Spring Boot route must match. The controller's response should also return JSON in a structure that your Android model can parse.&lt;/p&gt;

&lt;p&gt;Once the response arrives, you can pass the employee list to a RecyclerView.Adapter and display each employee inside a card or list item.&lt;/p&gt;

&lt;p&gt;Common Mistakes to Avoid&lt;/p&gt;

&lt;p&gt;Incorrect base URL: Make sure the server address and endpoint path are correct.&lt;/p&gt;

&lt;p&gt;Missing internet permission: Check AndroidManifest.xml if requests never reach the server.&lt;/p&gt;

&lt;p&gt;Mismatched JSON fields: Verify that the backend's JSON response matches your model class.&lt;/p&gt;

&lt;p&gt;Testing with localhost: The Android emulator's localhost refers to the emulator itself, not your development computer.&lt;/p&gt;

&lt;p&gt;Ignoring errors: Handle unsuccessful HTTP responses as well as network failures.&lt;/p&gt;

&lt;p&gt;Using insecure production URLs: Prefer HTTPS for production applications and never expose passwords, private keys, or sensitive tokens in source code.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;Retrofit makes it easier to connect an Android application to a REST API using Java. By separating the model class, API interface, and Retrofit client, we can keep our code cleaner and easier to maintain.&lt;/p&gt;

&lt;p&gt;Whether you are building an HRMS application, an e-commerce app, or a booking system, learning Retrofit is a valuable skill for Android development.&lt;/p&gt;

&lt;p&gt;Start with a simple GET request, verify the JSON response, and then move on to POST, PUT, and DELETE operations as your project grows.&lt;/p&gt;

&lt;p&gt;What API integration are you working on in your Android project? Share your experience in the comments.&lt;/p&gt;

</description>
      <category>android</category>
      <category>java</category>
      <category>bigneer</category>
    </item>
    <item>
      <title>Understanding REST APIs in Java with Spring Boot: A Beginner-Friendly Guide</title>
      <dc:creator>Dhananjay Bagul</dc:creator>
      <pubDate>Sat, 10 Oct 2026 03:46:21 +0000</pubDate>
      <link>https://dev.to/dhananjay_bagul/understanding-rest-apis-in-java-with-spring-boot-a-beginner-friendly-guide-2f64</link>
      <guid>https://dev.to/dhananjay_bagul/understanding-rest-apis-in-java-with-spring-boot-a-beginner-friendly-guide-2f64</guid>
      <description>&lt;p&gt;As a Java developer, understanding REST APIs is an important step toward building backend applications. REST APIs allow different applications to communicate with each other and exchange data over HTTP.&lt;/p&gt;

&lt;p&gt;In this article, we will understand what REST APIs are, how they work, and how to create a simple REST API using Java and Spring Boot.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What Is a REST API?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;REST stands for Representational State Transfer. A REST API allows clients, such as Android applications, web applications, or other services, to communicate with a server using HTTP requests.&lt;/p&gt;

&lt;p&gt;For example, an Android HRMS application might need to retrieve employee information from a backend server. The Android application sends a request to a REST API, and the server returns the requested data.&lt;/p&gt;

&lt;p&gt;A typical API response may look like this:&lt;/p&gt;

&lt;p&gt;{&lt;br&gt;
  "id": 1,&lt;br&gt;
  "name": "Rahul",&lt;br&gt;
  "department": "IT"&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;JSON is commonly used to exchange data between clients and servers.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Common HTTP Methods&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;REST APIs commonly use the following HTTP methods:&lt;/p&gt;

&lt;p&gt;GET — Retrieve data.&lt;/p&gt;

&lt;p&gt;POST — Create a new resource.&lt;/p&gt;

&lt;p&gt;PUT — Update an existing resource.&lt;/p&gt;

&lt;p&gt;PATCH — Partially update a resource.&lt;/p&gt;

&lt;p&gt;DELETE — Delete a resource.&lt;/p&gt;

&lt;p&gt;For example, an employee management application may use these endpoints:&lt;/p&gt;

&lt;p&gt;Method&lt;/p&gt;

&lt;p&gt;Endpoint&lt;/p&gt;

&lt;p&gt;Purpose&lt;/p&gt;

&lt;p&gt;GET&lt;/p&gt;

&lt;p&gt;/api/employees&lt;/p&gt;

&lt;p&gt;Retrieve employees&lt;/p&gt;

&lt;p&gt;GET&lt;/p&gt;

&lt;p&gt;/api/employees/1&lt;/p&gt;

&lt;p&gt;Retrieve one employee&lt;/p&gt;

&lt;p&gt;POST&lt;/p&gt;

&lt;p&gt;/api/employees&lt;/p&gt;

&lt;p&gt;Create an employee&lt;/p&gt;

&lt;p&gt;PUT&lt;/p&gt;

&lt;p&gt;/api/employees/1&lt;/p&gt;

&lt;p&gt;Update an employee&lt;/p&gt;

&lt;p&gt;DELETE&lt;/p&gt;

&lt;p&gt;/api/employees/1&lt;/p&gt;

&lt;p&gt;Delete an employee&lt;/p&gt;

&lt;p&gt;The actual behavior depends on how the backend implements each endpoint.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Why Use Spring Boot?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Spring Boot simplifies Java application development by providing auto-configuration, starter dependencies, and an embedded web server.&lt;/p&gt;

&lt;p&gt;For REST API development, the spring-boot-starter-web dependency provides the web framework features needed to create controllers and handle HTTP requests.&lt;/p&gt;

&lt;p&gt;You can create a project using Spring Initializr:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://start.spring.io/" rel="noopener noreferrer"&gt;https://start.spring.io/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select Java, choose a compatible Spring Boot version, and add the Spring Web dependency.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a Simple REST API&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's create a basic endpoint that returns a welcome message.&lt;/p&gt;

&lt;p&gt;Create a controller class:&lt;/p&gt;

&lt;p&gt;package com.example.demo.controller;&lt;/p&gt;

&lt;p&gt;import org.springframework.web.bind.annotation.GetMapping;&lt;br&gt;
import org.springframework.web.bind.annotation.RestController;&lt;/p&gt;

&lt;p&gt;@RestController&lt;br&gt;
public class HelloController {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@GetMapping("/api/hello")
public String hello() {
    return "Welcome to my first REST API!";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Understanding the Code&lt;/p&gt;

&lt;p&gt;@RestController&lt;/p&gt;

&lt;p&gt;This annotation marks the class as a REST controller. Return values are written to the HTTP response body.&lt;/p&gt;

&lt;p&gt;@GetMapping("/api/hello")&lt;/p&gt;

&lt;p&gt;This maps HTTP GET requests to the /api/hello endpoint.&lt;/p&gt;

&lt;p&gt;hello()&lt;/p&gt;

&lt;p&gt;This method returns a simple text response when the endpoint is requested.&lt;/p&gt;

&lt;p&gt;Run the Spring Boot application. If it starts successfully on the default port, open:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://localhost:8080/api/hello" rel="noopener noreferrer"&gt;http://localhost:8080/api/hello&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should see:&lt;/p&gt;

&lt;p&gt;Welcome to my first REST API!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Test the API Using Postman&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Postman is a useful tool for testing REST APIs without building a frontend first.&lt;/p&gt;

&lt;p&gt;Follow these steps:&lt;/p&gt;

&lt;p&gt;Open Postman.&lt;/p&gt;

&lt;p&gt;Create a new HTTP request.&lt;/p&gt;

&lt;p&gt;Select the GET method.&lt;/p&gt;

&lt;p&gt;Enter &lt;a href="http://localhost:8080/api/hello" rel="noopener noreferrer"&gt;http://localhost:8080/api/hello&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Click Send.&lt;/p&gt;

&lt;p&gt;The response body should contain the welcome message.&lt;/p&gt;

&lt;p&gt;You can also inspect the HTTP status code, response headers, and request details.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How an Android App Uses a REST API&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;REST APIs are particularly useful when developing Android applications.&lt;/p&gt;

&lt;p&gt;A typical request flow looks like this:&lt;/p&gt;

&lt;p&gt;The user opens an Android screen.&lt;/p&gt;

&lt;p&gt;The Android application sends an HTTP request to the backend.&lt;/p&gt;

&lt;p&gt;The Spring Boot server processes the request.&lt;/p&gt;

&lt;p&gt;The server returns a response, often in JSON format.&lt;/p&gt;

&lt;p&gt;The Android application reads the response and updates the user interface.&lt;/p&gt;

&lt;p&gt;Libraries such as Retrofit can simplify HTTP communication in Android applications.&lt;/p&gt;

&lt;p&gt;For example, an HRMS mobile application can use REST APIs to retrieve employee profiles, attendance information, or other employee-related data.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Best Practices for REST API Development&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Keep these principles in mind when developing backend APIs:&lt;/p&gt;

&lt;p&gt;Use clear and consistent endpoint names.&lt;/p&gt;

&lt;p&gt;Choose suitable HTTP methods and status codes.&lt;/p&gt;

&lt;p&gt;Validate incoming request data.&lt;/p&gt;

&lt;p&gt;Handle errors with meaningful responses.&lt;/p&gt;

&lt;p&gt;Keep controller and business logic appropriately separated.&lt;/p&gt;

&lt;p&gt;Avoid exposing passwords, private information, or internal server details.&lt;/p&gt;

&lt;p&gt;Test successful requests and failure scenarios with Postman.&lt;/p&gt;

&lt;p&gt;Use authentication and authorization for protected resources.&lt;/p&gt;

&lt;p&gt;For larger applications, a layered architecture with controller, service, and repository components can help keep the code organized and maintainable.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;REST APIs are an essential part of modern backend development. Java and Spring Boot provide a practical foundation for creating APIs that connect Android applications, web applications, databases, and other services.&lt;/p&gt;

&lt;p&gt;Start with simple GET endpoints, learn how POST and PUT requests work, and then practise building CRUD APIs with proper validation, database integration, and error handling.&lt;/p&gt;

&lt;p&gt;Consistent practice with Java, Spring Boot, REST APIs, MySQL, and Postman can help you develop the skills needed to build real-world backend applications.&lt;/p&gt;

&lt;h1&gt;
  
  
  java #springboot #restapi #beginners #backend
&lt;/h1&gt;

</description>
      <category>api</category>
      <category>beginners</category>
      <category>java</category>
      <category>springboot</category>
    </item>
  </channel>
</rss>
