Friday, March 23, 2012


Using Asynchronous task in Android


Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 
<Button
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/readWebpage"
    android:onClick="readWebpage"
    android:text="Load Webpage">
</Button>
<TextView
    android:id="@+id/TextView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="Asynchronous task Demo from CoderzHeaven">
</TextView>
</LinearLayout>
Here is the AndroidManifest.xml file
Make sure to add the android.permission.INTERNET permission in this file.
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
      package="com.pack.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AsynsTaskExample"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
    </application>
 
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
The main java file
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.pack.coderzheaven;
 
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;
import android.widget.Toast;
 
public class AsynsTaskExample 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) {
                    Toast.makeText(getApplicationContext(),"Some Error Occurred! " + e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
            return response;
        }
 
        @Override
        protected void onPostExecute(String result) {
            textView.setText(result);
            Toast.makeText(getApplicationContext(),"Loading WebPage Complete", Toast.LENGTH_LONG).show();
        }
    }
 
    public void readWebpage(View view) {
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://www.google.com" });
 
    }
}

No comments: