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

by JFrankie, May 8, 2013
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:

HttpURLConnection con = (HttpURLConnection) ( new URL(url)).openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
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.

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:

InputStream is = con.getInputStream();
byte[] b = new byte[1024];
while ( is.read(b) != -1)
  buffer.append(new String(b));
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.

private class SendHttpRequestTask extends AsyncTask<String, Void, String>{
  
  @Override
  protected String doInBackground(String... params) {
   String url = params[0];
   String name = params[1];
   String data = sendHttpRequest(url, name);
   return data;
  }

  @Override
  protected void onPostExecute(String result) {
   edtResp.setText(result);
   item.setActionView(null);   
  }
}

Running the app we get:


android_httpclient_post_get_1android_httpclient_post_get_2

As we can see we post a name to the server and it responds with the classic ‘Hello….’. On the server side we can check that the server received correctly our post parameter:

android_tomcat_post_log

Download data from server

One of the most common scenario is when an Android App has to download some data from a remote sever. We can suppose that we want to download an image from the server. In this case we have always to use an AsyncTask to complete our operation, the code is shown below:
public byte[] downloadImage(String imgName) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        System.out.println("URL ["+url+"] - Name ["+imgName+"]");
        
        HttpURLConnection con = (HttpURLConnection) ( new URL(url)).openConnection();
        con.setRequestMethod("POST");
        con.setDoInput(true);
        con.setDoOutput(true);
        con.connect();
        con.getOutputStream().write( ("name=" + imgName).getBytes());
        
        InputStream is = con.getInputStream();
        byte[] b = new byte[1024];
        
        while ( is.read(b) != -1)
            baos.write(b);
        
        con.disconnect();
    }
    catch(Throwable t) {
        t.printStackTrace();
    }
    
    return baos.toByteArray();
}

This method is called in this way:
private class SendHttpRequestTask extends AsyncTask<String, Void, byte[]> {

    
    @Override
    protected byte[] doInBackground(String... params) {
        String url = params[0];
        String name = params[1];
        
        HttpClient client = new HttpClient(url);
        byte[] data = client.downloadImage(name);
        
        return data;
    }

    @Override
    protected void onPostExecute(byte[] result) {
        Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length);
        imgView.setImageBitmap(img);
        item.setActionView(null);
        
    }
   
}

Running the app we have:

android_httpclient_post_download

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:
public void connectForMultipart() throws Exception {
    con = (HttpURLConnection) ( new URL(url)).openConnection();
    con.setRequestMethod("POST");
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setRequestProperty("Connection", "Keep-Alive");
    con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    con.connect();
    os = con.getOutputStream();
}

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).
public void addFormPart(String paramName, String value) throws Exception {
  writeParamData(paramName, value);
}

private void writeParamData(String paramName, String value) throws Exception {
    os.write( (delimiter + boundary + "\r\n").getBytes());
    os.write( "Content-Type: text/plain\r\n".getBytes());
    os.write( ("Content-Disposition: form-data; name=\"" + paramName + "\"\r\n").getBytes());;
    os.write( ("\r\n" + value + "\r\n").getBytes());
    
}

where
private String delimiter = "--";
private String boundary =  "SwA"+Long.toString(System.currentTimeMillis())+"SwA";
To add a file part we can use:
public void addFilePart(String paramName, String fileName, byte[] data) throws Exception {
    os.write( (delimiter + boundary + "\r\n").getBytes());
    os.write( ("Content-Disposition: form-data; name=\"" + paramName +  "\"; filename=\"" + fileName + "\"\r\n"  ).getBytes());
    os.write( ("Content-Type: application/octet-stream\r\n"  ).getBytes());
    os.write( ("Content-Transfer-Encoding: binary\r\n"  ).getBytes());
    os.write("\r\n".getBytes());
   
    os.write(data);
    
    os.write("\r\n".getBytes());
}

So in our app we have:
private class SendHttpRequestTask extends AsyncTask<String, Void, String> {

    
    @Override
    protected String doInBackground(String... params) {
        String url = params[0];
        String param1 = params[1];
        String param2 = params[2];
        Bitmap b = BitmapFactory.decodeResource(UploadActivity.this.getResources(), R.drawable.logo);
        
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        b.compress(CompressFormat.PNG, 0, baos);

        try {
            HttpClient client = new HttpClient(url);
            client.connectForMultipart();
            client.addFormPart("param1", param1);
            client.addFormPart("param2", param2);
            client.addFilePart("file", "logo.png", baos.toByteArray());
            client.finishMultipart();
            String data = client.getResponse();
        }
        catch(Throwable t) {
            t.printStackTrace();
        }
        
        return null;
    }

    @Override
    protected void onPostExecute(String data) {            
        item.setActionView(null);
        
    }
    
    
    
}

Running it we have:


android_tomcat_post_upload_logandroid_httpclient_post_upload
Source code @ github.

Android ListView animation

by JFrankie, April 22, 2013
We talked about ListView and Custom Adapter, but an aspect we want to consider is how to animate the ListView. In this example we will show how to animate Listview when we delete items using AnimatorListener.
As example we will use an ArrayAdapter that is populated by a large number of items. In this case we will use android.R.layout.simple_list_item_1 as row layout.
We will focus our attention on the item animation. For example we want that selected item will fade until they disappear.
The first thing we need to do is creating our animation. We will an xml file called fade_anim.xml under res/anim.
The xml file is very simple, we define that we want that our animation moves from fade 1 to fade 0 with a linear interpolator.

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromAlpha="1.0" android:toAlpha="0" 
       android:interpolator="@android:anim/linear_interpolator"
       android:duration="1000"/>
 
Now in the main activity we simply populate our ListView with an ArrayAdapter like:

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     ListView lv = (ListView) findViewById(R.id.listView);
     List<ItemDetail> itemList = createItems(50);
     final ArrayAdapter<ItemDetail> aAdpt = 
           new ArrayAdapter<ItemDetail>(this,                  
                  android.R.layout.simple_list_item_1, itemList);

     lv.setAdapter(aAdpt);
..
}

Where createItems method simply create items like item1,item2,...
The first thing we have to load is our animation in the onCreate with:

// Load animation
final Animation anim = AnimationUtils.loadAnimation(this, R.anim.fade_anim);

Now we want to start our animation when user clicks on an item inside the ListView. But we need first run the animation and when the animation ends we have to remove item from the list. We need, then, an AnimationListener to coordinate these phases (line 6).
So we have first create the listener and then start the animation (line 21) like:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, 
                        final View view, final int position,
   long id) {
   anim.setAnimationListener(new Animation.AnimationListener() {               

                         @Override
                  public void onAnimationStart(Animation animation) {
                  }
       
                         @Override
                  public void onAnimationRepeat(Animation animation) {}
     
                 @Override
                 public void onAnimationEnd(Animation animation) {
      ItemDetail item = aAdpt.getItem(position);
      aAdpt.remove(item);
                }
              });
          view.startAnimation(anim);
 }
});

Let's run the app. If we clicks on an item we will see that it works correctly, first starts the fading animation and then the item is removed.
Come back to xml file and make the animation duration longer  (for example 3sec=3000), and run it again. While we click on an item and it starts fading let's scroll the list.
What is happening?
Well we can notice that other items are fading even if we didn't click them at all!! Why??
This is because OS re-use the view with other items. So we need to find a way to tell the to OS that while the item is fading the corresponding view shouldn't be used.
How can we do it?!
Well in the onAnimationStart (line 10), when the animation is starting, we can flag the view with transient state, that tells to the OS to not re-use the view.
When the animation ends (line 20), we tell to the OS that the view can be re-used and set the transient to the default value while we set the fade value to 1.

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, 
                        final View view, final int position,
   long id) {
   anim.setAnimationListener(new Animation.AnimationListener() {               

                         @Override
                  public void onAnimationStart(Animation animation) {
                            view.setHasTransientState(true);
                  }
       
                         @Override
                  public void onAnimationRepeat(Animation animation) {}
     
                 @Override
                 public void onAnimationEnd(Animation animation) {
                            ItemDetail item = aAdpt.getItem(position);
                            aAdpt.remove(item);
                            view.setHasTransientState(false);
                }
              });
          view.startAnimation(anim);
 }
});

Now everything works correctly even if we scroll the ListView while the item is fading.

Source  @ github

Android ListView context menu: ActionMode.CallBack

by JFrankie, April 15, 2013
In this post we want to analyze the context menu (contextual action bar). This is a menu that is related to a specific item. The contextual menu can be applied to almost all views but it is usually used with ListView. We talk a lot about List view, because it is one of the most important component. We can distinguish two different type of contextual menu:
  • Floating menu
  • Contextual action mode (ActionMode)
The floating menu is used with Android version lower than 3.0 (API level 11). It is essentially a menu that appears when an user long click on an ListView item. You can find an example here. It looks like the image shown below:

The contextual action mode is introduced in Android 3.0 or higher and it is essentially a contextual bar that appears on the top when user long clicks an item. According to Android guides this kind of menu is better than the floating menu. In this post we want to analyze how we can create this menu.



Create contextual action Mode: Define ActionMode.CallBack interface

To create a contextual menu we have first to define a ActionMode.CallBack interface. This interface is called when an user long clicks on an ListView item. The code looks like:

 private ActionMode.Callback modeCallBack = new ActionMode.Callback() {
  
   public boolean onPrepareActionMode(ActionMode mode, Menu menu)    
    return false;
   }
 
  public void onDestroyActionMode(ActionMode mode) {
    mode = null;   
   }
  
   public boolean onCreateActionMode(ActionMode mode, Menu menu) {
     return true;
   }
  
   public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
   }
};
 
We are interested on line 11 and line 15. The first one is where we will create our contextual action bar on the top of the screen and in line 15 is where we handle the logic when user chooses one of our menu item.
The first thing we have to do is creating our menu. For simplicity we can suppose we have just two menu items, then we define a file under res/menu called activity_main.xml:


<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/edit"
          android:icon="@android:drawable/ic_menu_edit"/>
      
    <item android:id="@+id/delete"
          android:icon="@android:drawable/ic_menu_delete"/>
    
</menu>

Now we have our menu and we simply have to "inject" it in the onCreateActionMode method.

public boolean onCreateActionMode(ActionMode mode, Menu menu) {
 mode.setTitle("Options");
 mode.getMenuInflater().inflate(R.menu.activity_main, menu);
 return true;
}

Now we have to show this contextual action bar when user long clicks on an item.

ActionMode and Long Click: onItemLongClickListener

If we want to show this contextual bar when user long clicks we have simply set a listener on our ListView, that we call lv in the source code. So we have:


lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
   public boolean onItemLongClick (AdapterView parent, View view, int position, long id) {
     System.out.println("Long click");
     startActionMode(modeCallBack);
     view.setSelected(true);
     return true;
   }
});

In line 4 we simply start the contextual menu using startActionMode method. Now the result is:
As you can see in the top we have our contextual action bar.

Contextual menu item selection


Now let's suppose we an user clicks on a menu item. How do we handle this event? Well if we come back at ActionMode.CallBack we have to implement another method onActionItemClicked. So we have:


public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
   
 int id = item.getItemId();
 switch (id) {
   case R.id.delete: {
     aAdpt.remove( aAdpt.getItem(aAdpt.currentSelection) );
            mode.finish();
     break;
          }
          case R.id.edit: {
     System.out.println(" edit ");
            break;
         }
         default:
            return false;

}

In line 6 we simply remove from our adapter the selected item. To know the position of the selected item inside the ListView we store it in the OnItemLongClickListener method.

aAdpt.currentSelection = position;

When we finish handling user menu item selection we have to dismiss the contextual action bar callig mode.finish (line 7).

Android listview background row style: Rounded Corner, alternate color

by JFrankie, April 8, 2013
One aspect we didn’t consider in the previous posts is how we can apply style or background to the Listview items (or row). We can customize the look of the ListView in the way we like, for example with can use as background rounded corner, alternate color and so on. By now we considered just custom adapter without taking into account how we can customize how each item inside the listview appears.
In this post we want to describe how we can use resource to customize the item look. The first example will describe how we can create rounded corners for each item inside a listview. In the second example we will show how we can alternate the background color.

ListView with rounded corner

Let’s suppose we want to create rounded corner for each item. How can we do this?…We need to create some drawable resources and apply them to each item. As you already know we have to create a custom adapter to implement this behaviour. In this post we don’t want to spend too much words about adapters because we described them here and here.
As we said the first thing we need is a drawable resource. As you may already know this is powerful feature of Android because it permits us to create geometrical figure in XML style. We have to specify some information to create this figure:
  • border size and color
  • background color (in our case a solid color)
  • corners
We need to create a file XML under the res/drawable directory. Let’s call this file rounded_corners.xml. This file contains a shape definition. A shape is a geometrical figure that is described by other tags:
  • stroke – a stroke line for the shape (witdh, color, dashWidth and dashGap)
  • solid – solid colour that fills the shape
  • corners – radius and so o
So the rounded_corners.xml look like:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <solid android:color="#00FF00"/>

    <corners android:radius="5dp" />
    
    <padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
    
    <stroke android:width="3dp" android:color="#00CC00"/>
</shape>

Once we have create our shape we need to apply it to the items. To do it we have to create another XML file that describe how we apply this shape. In this case we use the XML tag selector to specify when and how to apply the shape. To specify when to apply the shape we use the status. We specify to apply this shape when:

  • status = enable
  • status = selected
  • status = pressed

So our file (listview_selector.xml) looks like:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/rounded_corner" android:state_enabled="true"/>

    <item android:drawable="@drawable/rounded_corner" android:state_pressed="true"/>
    
    <item android:drawable="@drawable/rounded_corner" android:state_focused="true"/>
    
</selector>

Now we have defined our resource, we simply need to specify to apply it in our adapter in this way:
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    
    PlanetHolder holder = new PlanetHolder();
    
    // First let's verify the convertView is not null
    if (convertView == null) {
        // This a new view we inflate the new layout
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.row_layout, null);
        // Now we can fill the layout with the right values
        TextView tv = (TextView) v.findViewById(R.id.name);
        
        holder.planetNameView = tv;

        v.setTag(holder);
        
        v.setBackgroundResource(R.drawable.rounded_corner);
    }
    else 
        holder = (PlanetHolder) v.getTag();
    
    Planet p = planetList.get(position);
    holder.planetNameView.setText(p.getName());
    
    return v;
}

If we run the app we have:

android_listview_row_style

ListView with alternate color


As we describe above, if we want to change how each row look like inside the ListView we have simply change the resource and we can customize its look.

For example we can suppose we want to alternate the row color. In this case we need to create two drawable resource one for each background, like that:

even_row.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <solid android:color="#A0A0A0"/>

    <padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
    
    <stroke android:width="1dp" android:color="#00CC00"/>
</shape>

odd_row.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <solid android:color="#F0F0F0"/>

    <padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
    
    <stroke android:width="1dp" android:color="#00CC00"/>
</shape>

We need moreover two selectors that uses the drawable resources, like that

listview_selector_even.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/even_row" android:state_enabled="true"/>

    <item android:drawable="@drawable/even_row" android:state_pressed="true"/>
    
    <item android:drawable="@drawable/even_row" android:state_focused="true"/>
    
</selector>

listview_selector_odd.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/odd_row" android:state_enabled="true"/>

    <item android:drawable="@drawable/odd_row" android:state_pressed="true"/>
    
    <item android:drawable="@drawable/odd_row" android:state_focused="true"/>
    
</selector>

And finally we apply them inside our custom adapter:
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    
    PlanetHolder holder = new PlanetHolder();
    
    // First let's verify the convertView is not null
    if (convertView == null) {
        // This a new view we inflate the new layout
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.row_layout, null);
        // Now we can fill the layout with the right values
        TextView tv = (TextView) v.findViewById(R.id.name);
        
        holder.planetNameView = tv;

        v.setTag(holder);
        
        if ( position % 2 == 0)
          v.setBackgroundResource(R.drawable.listview_selector_even);
        else
            v.setBackgroundResource(R.drawable.listview_selector_odd);
    }
    else 
        holder = (PlanetHolder) v.getTag();
    
    Planet p = planetList.get(position);
    holder.planetNameView.setText(p.getName());
    
    return v;
}

Running the app we have:

android_listview_row_style_alternate

Source code @ github