Using AsyncTask in Android Application
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="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:

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