The class AsyncTask encapsulates the creation of Threads and Handlers. You must implement the method doInBackground (), which defines what action should be done in the background. This method is being automatically run in a separate Thread. To update the UI you can override the method onPostExecute (). This method will be called by the framework once your action is done and runs within the UI thread.

In this article I am going to explain how to use AsyncTask in an android application. In this example we will use AsyncTask to download the content of the webpage.

·         Start a new project named AsyncTaskDemo.

·         Open res/values/strings.xml and add the following element in the resources tag.

<string name="readWebpage">readWebpage</string>

·         Open AndroidManifest.xml and add the following permission:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

·         Now set the layout of an application in the res/layout/main.xml file:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""
        <Button android:layout_height="wrap_content"
                 android:text="Load Webpage" />
        <TextView android:id="@+id/TextView01"
                   android:text="Example Text" />

·         Open the Activity file and insert the following code:


import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class AsyncActivity extends Activity {

        private TextView textView;

        public void onCreate(Bundle savedInstanceState) {
               textView = (TextView) findViewById(;

        private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
               protected String doInBackground(String... urls) {
                     String response = "";
                      for (String url : urls) {
                           DefaultHttpClient client = new DefaultHttpClient();
                           HttpGet httpGet = new HttpGet(url);
                            try {
                                  HttpResponse execute = client.execute(httpGet);
                                  InputStream content =

                                  BufferedReader buffer = new BufferedReader(
                                                 new InputStreamReader(content));
                                  String s = "";
                                   while ((s = buffer.readLine()) != null) {
                                         response += s;
                           } catch (Exception e) {
                      return response;

               protected void onPostExecute(String result) {

        public void readWebpage(View view) {
              DownloadWebPageTask task = new DownloadWebPageTask();
              task.execute(new String[] { "" });


·         Run the application.

The output should look like below:

Using AsyncTask in Android Application

When you click on the button (Load Webpage) the content of the website (google in this) will display in the TextView.

  Modified On Mar-23-2018 02:20:13 AM

Leave Comment