Tutorial details

Android HTTP Client: GET, POST, Download, Upload, Multipart Request | App Code for Sale | Preview

Android HTTP Client: GET, POST, Download, Upload, Multipart Request | Android Tutorial

How to use HttpURLConnection to communicate with a remote server

Overview PAGE TOP

Often Android apps have to exchange information with a remote server. The easiest way is to use the HTTP protocol as base to transfer information. There are several scenarios where the HTTP protocol is very useful like downloading an image from a remote server or uploading some binary data to the server. Android app performs GET or POST request to send data. In this post, we want to analyze how to use HttpURLConnection to communicate with a remote server. We will cover three main topics:

  • GET and POST requests
  • Download data from the server
  • Upload data to the server using MultipartRequest

As a server we will use three simple Servlet running inside Tomcat 7.0. We won’t cover how to create a Servlet using API 3.0 but the source code will be available soon.

GET and POST requests

GET and POST requests are the base blocks in HTTP protocol. To make this kind of requests we need first to open a connection toward the remove server:

1   HttpURLConnection con = (HttpURLConnection) ( new URL(url)).openConnection();
2   con.setRequestMethod("POST");
3   con.setDoInput(true);
4   con.setDoOutput(true);
5   con.connect();

In the first line we get the HttpURLConnection, while in the line 2 we set the method and at the end we connect to the server.

Once we have opened the connection we can write on it using the OutputStream.

1   con.getOutputStream().write( ("name=" + name).getBytes());

As we already know parameters are written using key value pair.

The last step is reading the response, using the InputStream:

1   InputStream is = con.getInputStream();
2   byte[] b = new byte[1024];
3   while ( is.read(b) != -1)
4     buffer.append(new String(b));
5   con.disconnect();

Everything is very simple by now, but we have to remember one thing: making an HTTP connection is a time consuming operation that could require long time sometime so we can’t run it in the main thread otherwise we could get a ANR problem. To solve it we can use an AsyncTask.

01  private class SendHttpRequestTask extends AsyncTask<String, Void, String>{
03    @Override
04    protected String doInBackground(String... params) {
05     String url = params[0];
06     String name = params[1];
07     String data = sendHttpRequest(url, name);
08     return data;
09    }
11    @Override
12    protected void onPostExecute(String result) {
13     edtResp.setText(result);
14     item.setActionView(null);  
15    }
16  }

Running the app we get:


Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.