Understanding HTTP in Android

The HTTP (HyperText Transfer Protocol) is a fundamental communication protocol that allows different systems and devices to interconnect and exchange data over a network — primarily the Internet.

Every connected device implements this protocol at some level, enabling apps to send and receive information through APIs (Application Programming Interfaces) provided by their underlying platform.


HTTP in Android

In Android, the operating system provides built-in APIs to handle HTTP communication.
These APIs allow Android apps to send requests to remote servers and receive responses through an internet connection.

Example:

When an Android app needs to fetch weather data, it uses the platform’s HTTP APIs (like HttpURLConnection) to send a request to a weather API endpoint and process the response.


HTTP Protocol Basics

The HTTP specification defines several key components that make communication possible:

  • Request – Sent by the client (your app) to the server.
  • Response – Sent back by the server with data or status.
  • URL – The endpoint address to which the request is sent.
  • Parameters – Optional data sent with the request (e.g., query params).
  • Body – The main content (especially in POST requests).
  • Error Codes – Status indicators like 404 (Not Found) or 500 (Server Error).

Android’s HTTP API Options

Android has implemented the HTTP protocol through two main sets of APIs:

  1. java.net package – Modern and recommended.
  2. Apache HttpClient – Deprecated from Android 5.0 (Lollipop) onwards.

Using java.net APIs

The java.net package provides the standard Java classes for HTTP operations.

Key Classes:

  • HttpURLConnection – Manages the HTTP connection.
  • URL – Represents the web address.
  • InputStream, InputStreamReader, and BufferedReader – Used for reading the response.

Example Code:

try {
    URL url = new URL("http://google.co.in");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    InputStream stream = connection.getInputStream();
    InputStreamReader reader = new InputStreamReader(stream);
    BufferedReader bufferedReader = new BufferedReader(reader);

    String line;
    while ((line = bufferedReader.readLine()) != null) {
        System.out.println(line);
    }

    bufferedReader.close();
    connection.disconnect();
} catch (Exception e) {
    e.printStackTrace();
}

Required Permissions in AndroidManifest.xml

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

Using Apache HttpClient (Deprecated)

Before Android 5.0, developers often used Apache HttpClient for HTTP operations.
Though deprecated now, understanding it helps when maintaining legacy projects.

Steps:

1. Instantiate the Client

HttpClient client = new DefaultHttpClient();

2. Create an HTTP Request

HttpGet getRequest = new HttpGet("http://google.com");
getRequest.setHeader("param_name", "param_value");

Note: GET requests do not have a request body.
For POST requests, use HttpPost and attach parameters to the body.

3. Execute the Request and Handle the Response

HttpResponse response = client.execute(getRequest);
InputStream stream = response.getEntity().getContent();
// Read data from InputStream

You can also inspect response headers and content:

Header[] headers = response.getAllHeaders();
for (Header header : headers) {
    System.out.println(header.getName() + ": " + header.getValue());
}

Summary

FeatureDescription
PurposeEnables apps to communicate with remote servers via HTTP protocol
Modern APIHttpURLConnection from java.net
Deprecated APIApache HttpClient (deprecated since Android 5.0)
Required PermissionsINTERNET, ACCESS_NETWORK_STATE
Common HTTP MethodsGET, POST, PUT, DELETE
Response HandlingVia InputStream and Reader classes

References


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top