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

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
	protected void onPreExecute() {
		this.dialog.setMessage("Getting data...");;
	//Can use UI thread here
	protected void onPostExecute(ArrayList<hashmap <String, String>> list) {
		if (this.dialog.isShowing()) {
		String[] columns = new String[] { "name", "lastname", "email" };
		int[] renderTo = new int[] {,, };
		ListAdapter listAdapter = new SimpleAdapter(ManageCustomersActivity.this, list, R.layout.manage_customers_list_row, columns, renderTo);
	//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.

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

Hope this examples helps a bit. Cheers.

You made it all the way down here so you must have enjoyed this post! You may also like:

From PHP to Android, the simple stuff – Intents and the POST-GET analogy Branko Ajzele
Branko Ajzele, | 2

From PHP to Android, the simple stuff – Intents and the POST-GET analogy

From PHP to Android, the simple foreach loop example Branko Ajzele
Branko Ajzele, | 0

From PHP to Android, the simple foreach loop example

From PHP to Android, the simple stuff – What about arrays Branko Ajzele
Branko Ajzele, | 8

From PHP to Android, the simple stuff – What about arrays


  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(
    can you please elp 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>.

Tell us about your project

Drop us a line. We'd love to know more about your project.