Pages

Sunday, March 17, 2013

Simple AsyncTask

XML
Don't forget to Add internet permission.




<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>
</ScrollView>
</LinearLayout>



JAVA 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

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

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

public class MainActivity extends Activity {
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button)findViewById(R.id.button1);
tv = (TextView)findViewById(R.id.textView1);
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// create an instance of async class .Since the name of my class is mehvish ,i have used mehvish over here.
mehvish me = new mehvish();
me.execute("http://www.facebook.com");

}
});


}

public class mehvish extends AsyncTask<String, Void, String>
{
ProgressDialog pd = new ProgressDialog(MainActivity.this);
protected void onPreExecute(){
pd.setMessage("Loading...");
pd.show();

}

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
BufferedReader rd = null ;
String data = null ;


HttpClient hc = new DefaultHttpClient();
try {

URI uri = new URI(params[0]);
HttpGet hg = new HttpGet(uri);
HttpResponse hr = hc.execute(hg);


InputStream is = hr.getEntity().getContent();
rd= new BufferedReader(new InputStreamReader(is));

StringBuffer buffer = new StringBuffer("");
String line = "";
String newLine = System.getProperty("line.separator");
while ((line= rd.readLine())!=null)
{
buffer.append(line + newLine);

}
rd.close();
data = buffer.toString();
return data;
}



catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


finally {
if (rd != null) {
try {
rd.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
return null;



}
protected void onPostExecute(String result){
pd.dismiss();

super.onPostExecute(result);
tv.setText(result);

}
}
}

No comments:

Post a Comment