From PHP to Android, the simple stuff – AsyncTask and the AJAX analogy

Featured Image

As a web developer you have surely heard for AJAX. Its one of those cool things that give your web app more dynamic interface by fetching various data behind the scene. So, whats the analogy with the Android platform?

Well, in Android, or better yet Java development you have more robust solution for doing things “behind the scene”. This solution is called threading. Not to go to deeply in explaining, consider it as a parallel mini app/code that executes along other code stuff enabling more fluid app behavior without any (or without too much) non responsive interfaces.

In Android there is a really neat solution for doing threaded code, its called AsyncTask. Its a special class that you extend, one that enables you proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

Below is a practical example on how to do a call to web service, fetch some “array like” data and pass them to ListView.

//...
private class ApiCallCustomerList extends AsyncTask<string , Void, ArrayList<HashMap<String, String>>> {
 
	private final ProgressDialog dialog = new ProgressDialog(ManageCustomersActivity.this);
 
	//Can use UI thread here
	@Override
	protected void onPreExecute() {
		this.dialog.setMessage("Getting data...");
		this.dialog.show();
	}
 
	//Can use UI thread here
	@Override
	protected void onPostExecute(ArrayList<hashmap <String, String>> list) {
 
		if (this.dialog.isShowing()) {
			this.dialog.dismiss();
		}
 
		String[] columns = new String[] { "name", "lastname", "email" };
		int[] renderTo = new int[] { R.id.name, R.id.lastname, R.id.email };
		ListAdapter listAdapter = new SimpleAdapter(ManageCustomersActivity.this, list, R.layout.manage_customers_list_row, columns, renderTo);
 
		setListAdapter(listAdapter);
		registerForContextMenu(getListView());
	}
 
	//Can't use UI thread here
	protected ArrayList</hashmap><hashmap <String, String>> doInBackground(String... params) {
 
		//My example passes SharedPreferences object to my custom ApiCall class constructor. 
		ApiCall apiCall = new ApiCall(PreferenceManager.getDefaultSharedPreferences(ManageCustomersActivity.this));
		ArrayList</hashmap><hashmap <String, String>> list = apiCall.customerList();
 
 
		return list;
	}
}
//...

As you can see, some things are specific to “Can use UI thread here” methods. For example, using the “setListAdapter(listAdapter);” inside the “doInBackground” method would cause an error. If you are PHP developer like me and just starting Android, I would strongly suggest to study the AsyncTask documentation provided here.

In PHP development, or more precisely in JavaScript we would do something similar with AJAX like shown in example below.

//...
$.ajax({
type: "POST",
url: "some.php",
success: function(list){
	//list data here
}
});
//...

Hope this examples helps a bit. Cheers.


2 comments

  1. Hi Branko!
    Thanks for the threading concept.but i cant understand it properly.My project is to show a mysql table in te android mobile.I got it.but my problem is the data in the android mobile must be changed dynamically,I use this php file(192.168.100.9:8000/sample1.php).
    can you please elp me.how can i achieve this

  2. Thanks, this helped me by identifying which methods could touch the UI.

    I am now trying to modify it to use a progress dialog in the title bar so as not to interfere with other user actions.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.