---
title: "Using AsyncTask in Android Application"  
description: "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"  
author: "Chris Anderson"  
published: 2011-10-31  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/799/using-asynctask-in-android-application  
category: "android"  
tags: ["android"]  
reading_time: 5 minutes  

---

# Using AsyncTask in Android Application

The class [AsyncTask](https://www.mindstick.com/forum/158886/what-is-the-purpose-of-asynctask-in-android-and-when-should-it-be-used) encapsulates the [creation](https://yourviews.mindstick.com/view/85506/what-was-the-causes-of-creation-of-pakistan) 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](https://yourviews.mindstick.com/view/81101/time-for-improvement-in-healthcare-facility-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](https://www.mindstick.com/articles/1653/creating-libraries-for-android-applications). In this example we will use AsyncTask to [download](https://www.mindstick.com/blog/114673/download-hungry-shark-evolution-mod-apk-unlimited-money) the content of the webpage.

· Start a new project named AsyncTaskDemo.

· Open **res/values/strings.xml** and add the following element in the [resources](https://www.mindstick.com/interview/1265/what-are-the-different-types-of-resources-through-which-cold-fusion-can-communicate) tag.

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

· Open **[AndroidManifest.xml](https://www.mindstick.com/interview/12768/what-is-the-androidmanifest-xml)** and add the following [permission](https://answers.mindstick.com/qa/33390/how-to-enable-api-access-in-salesforce-by-permission-set):

```
<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="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
        <Button android:layout_height="wrap_content"
                 android:layout_width="match_parent"
                 android:id="@+id/readWebpage"
                 android:onClick="readWebpage"
                 android:text="Load Webpage" />
        <TextView android:id="@+id/TextView01"
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:text="Example Text" />
</LinearLayout>
```

· Open the Activity file and insert the following code:

```
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

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

public class AsyncActivity extends Activity {

        private TextView textView;

        @Override
        public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
               textView = (TextView) findViewById(R.id.TextView01);
       }

        private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
               @Override
               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 =
                                               execute.getEntity().getContent();

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

               protected void onPostExecute(String result) {
                      textView.setText(result);
              }
       }

        public void readWebpage(View view) {
              DownloadWebPageTask task = new DownloadWebPageTask();
              task.execute(new String[] { "http://www.google.co.in/" });

       }
}
```

· Run the application.\

##### The output should look like below:

![Using AsyncTask in Android Application](https://www.mindstick.com/mindstickarticle/c25f0596-db32-4a79-bda4-38535f3b8dc0/images/735d70d3-d7f2-4229-86c3-70a51bfd153b.png)

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

\

\

\

---

Original Source: https://www.mindstick.com/articles/799/using-asynctask-in-android-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
