Android HttpURLConnection Example

Connecting Android application to a remote server is the most common task in the android application development. Android HttpURLConnection client is the preferred method to connect to a remote server through HTTP. All the network calls use the HTTP to send and receive data.

We will create a sample project to demonstrate how HttpURLConnection in android works..

Android HttpURLConnection client supports TLS, streaming uploads and downloads, configurable timeouts, IPv6, and connection pooling.

Read more on Android Official Website

How to make HTTP request using Get and Post Method in Android

We will create the sample project in order to implement HttpURLConnection class in our project. We will use Get and Post methods in this example.

Download Source Code
Create a new Project in Android Studio with default settings and name it HttpExample.

We need Internet permission for performing networks operations. Add Internet permission in AndroidMenifest file.

Adding a TextView and Two Buttons in activity layout file. We will display the response from the server into TextView. Buttons will be used to send Post and Get request. Below is the code of main activity layout file.


Modify your main activity file as per below code. We have used HttpURLConnection class within AsyncTask to perform network operations on a separate thread without blocking UI.

In the onCreate method, we have initialized the UI elements TextView and Buttons. Added onClickListener for both buttons.

HttpURLConnection Get Request

ByGetMethod will send the get HTTP request to the remote server. It will return the response from the server in the InputStream format.

HttpURLConnection Post Request

ByPostMethod will send the post HTTP request to the remote server. It will return the response from the server in the InputStream format.

Converting InputSteam into String

ConvertStreamToString() method will accept InputStream and convert it into string format. This method will return a string.

Using HttpURLConnection within AsyncTask

We have create a AsyncTask class MakeNetworkCall. It will call the ByPostMethod and ByGetMethod on background thread without blocking the main thread. we are also updating TextView in onPreExecute and onPostExecute.

We have a php file on the server to communicate with our android application. Android application is ending HTTP post and Get request to http.php. Below is the code of http.php

Finally, Application is ready, Running the application will generate following outputs for Get and Post method respectively.

android httpurlconnection get android httpurlconnection post

Android HttpURLConnection Example – Video Demo

This video is showing the project structure in Android Studio and application output on an emulator.

Android HttpURLConnection – More Examples

I hope you will like this tutorial on Android httpURLConnection. Subscribe to our Newsletter to get new articles and tutorials in your inbox as soon as they published. You can connect with us on Facebook and also subscribe to our Youtube Channel.

Check Also

asynctask kotlin example

Kotlin AsyncTask Tutorial with Example

Here is an example of implementing AsyncTask in Kotlin. I will create an Android application …

  • Chinmay Gangal

    thank you soo much !!