Blog : Android HTTP Client: GET, POST, Download, Upload, Multipart Request
Android HTTP Client: GET, POST, Download, Upload, Multipart Request
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{
02
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
}
10
11
@Override
12
protected void onPostExecute(String result) {
13
edtResp.setText(result);
14
item.setActionView(null);
15
}
16
}
Upload data to the server using MultipartRequest
This the most complex part in handling http connection. Natively HttpURLConnection doesn’t handle this type of request. It can happen that an Android App has to upload some binary data to the server. It can be that an app has to upload an image for example. In this case the request get more complex, because a “normal” request isn’t enough. We have to create a MultipartRequest.
A MultipartRequest is a request that is made by different parts like parameters and binary data. How can we handle this request?
Well the first step is opening a connection informing the server we wants to send some binary info:
01
public void connectForMultipart() throws Exception {
02
con = (HttpURLConnection) ( new URL(url)).openConnection();
03
con.setRequestMethod("POST");
04
con.setDoInput(true);
05
con.setDoOutput(true);
06
con.setRequestProperty("Connection", "Keep-Alive");
07
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
08
con.connect();
09
os = con.getOutputStream();
10
}
In the line 6 and 7 we specify the request content-type and another field called boundary. This field is a char sequence used to separate different parts.
For each part we want to add we need to specify if it is text part like post parameter or it is a file (so binary data).
01
public void addFormPart(String paramName, String value) throws Exception {
02
writeParamData(paramName, value);
03
}
04
05
private void writeParamData(String paramName, String value) throws Exception {
06
os.write( (delimiter + boundary + "\r\n").getBytes());
07
os.write( "Content-Type: text/plain\r\n".getBytes());
08
os.write( ("Content-Disposition: form-data; name=\"" + paramName + "\"\r\n").getBytes());;
09
os.write( ("\r\n" + value + "\r\n").getBytes());
10
11
}
where
1
private String delimiter = "--";
2
private String boundary = "SwA"+Long.toString(System.currentTimeMillis())+"SwA";
To add a file part we can use:
01
public void addFilePart(String paramName, String fileName, byte[] data) throws Exception {
02
os.write( (delimiter + boundary + "\r\n").getBytes());
03
os.write( ("Content-Disposition: form-data; name=\"" + paramName + "\"; filename=\"" + fileName + "\"\r\n" ).getBytes());
04
os.write( ("Content-Type: application/octet-stream\r\n" ).getBytes());
05
os.write( ("Content-Transfer-Encoding: binary\r\n" ).getBytes());
06
os.write("\r\n".getBytes());
07
08
os.write(data);
09
10
os.write("\r\n".getBytes());
11
}
So in our app we have:
01
private class SendHttpRequestTask extends AsyncTask
{
02
03
@Override
04
protected String doInBackground(String... params) {
05
String url = params[0];
06
String param1 = params[1];
07
String param2 = params[2];
08
Bitmap b = BitmapFactory.decodeResource(UploadActivity.this.getResources(), R.drawable.logo);
09
10
ByteArrayOutputStream baos = new ByteArrayOutputStream();
11
b.compress(CompressFormat.PNG, 0, baos);
12
13
try {
14
HttpClient client = new HttpClient(url);
15
client.connectForMultipart();
16
client.addFormPart("param1", param1);
17
client.addFormPart("param2", param2);
18
client.addFilePart("file", "logo.png", baos.toByteArray());
19
client.finishMultipart();
20
String data = client.getResponse();
21
}
22
catch(Throwable t) {
23
t.printStackTrace();
24
}
25
26
return null;
27
}
28
29
@Override
30
protected void onPostExecute(String data) {
31
item.setActionView(null);
32
33
}
34
35
}
Running it we have:Upload data to the server using MultipartRequest
This the most complex part in handling http connection. Natively HttpURLConnection doesn’t handle this type of request. It can happen that an Android App has to upload some binary data to the server. It can be that an app has to upload an image for example. In this case the request get more complex, because a “normal” request isn’t enough. We have to create a MultipartRequest.
A MultipartRequest is a request that is made by different parts like parameters and binary data. How can we handle this request?
Well the first step is opening a connection informing the server we wants to send some binary info:
01
public void connectForMultipart() throws Exception {
02
con = (HttpURLConnection) ( new URL(url)).openConnection();
03
con.setRequestMethod("POST");
04
con.setDoInput(true);
05
con.setDoOutput(true);
06
con.setRequestProperty("Connection", "Keep-Alive");
07
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
08
con.connect();
09
os = con.getOutputStream();
10
}
In the line 6 and 7 we specify the request content-type and another field called boundary. This field is a char sequence used to separate different parts.
For each part we want to add we need to specify if it is text part like post parameter or it is a file (so binary data).
01
public void addFormPart(String paramName, String value) throws Exception {
02
writeParamData(paramName, value);
03
}
04
05
private void writeParamData(String paramName, String value) throws Exception {
06
os.write( (delimiter + boundary + "\r\n").getBytes());
07
os.write( "Content-Type: text/plain\r\n".getBytes());
08
os.write( ("Content-Disposition: form-data; name=\"" + paramName + "\"\r\n").getBytes());;
09
os.write( ("\r\n" + value + "\r\n").getBytes());
10
11
}
where
1
private String delimiter = "--";
2
private String boundary = "SwA"+Long.toString(System.currentTimeMillis())+"SwA";
To add a file part we can use:
01
public void addFilePart(String paramName, String fileName, byte[] data) throws Exception {
02
os.write( (delimiter + boundary + "\r\n").getBytes());
03
os.write( ("Content-Disposition: form-data; name=\"" + paramName + "\"; filename=\"" + fileName + "\"\r\n" ).getBytes());
04
os.write( ("Content-Type: application/octet-stream\r\n" ).getBytes());
05
os.write( ("Content-Transfer-Encoding: binary\r\n" ).getBytes());
06
os.write("\r\n".getBytes());
07
08
os.write(data);
09
10
os.write("\r\n".getBytes());
11
}
So in our app we have:
01
private class SendHttpRequestTask extends AsyncTask {
02
03
@Override
04
protected String doInBackground(String... params) {
05
String url = params[0];
06
String param1 = params[1];
07
String param2 = params[2];
08
Bitmap b = BitmapFactory.decodeResource(UploadActivity.this.getResources(), R.drawable.logo);
09
10
ByteArrayOutputStream baos = new ByteArrayOutputStream();
11
b.compress(CompressFormat.PNG, 0, baos);
12
13
try {
14
HttpClient client = new HttpClient(url);
15
client.connectForMultipart();
16
client.addFormPart("param1", param1);
17
client.addFormPart("param2", param2);
18
client.addFilePart("file", "logo.png", baos.toByteArray());
19
client.finishMultipart();
20
String data = client.getResponse();
21
}
22
catch(Throwable t) {
23
t.printStackTrace();
24
}
25
26
return null;
27
}
28
29
@Override
30
protected void onPostExecute(String data) {
31
item.setActionView(null);
32
33
}
34
35
}
Running it we have: