/*******************ReadWebpageAsyncTaskActivity*************************/
package com.asynctask.android;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
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.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ReadWebpageAsyncTaskActivity extends Activity {
private TextView textView;
Button btn_readWebpage;
Button btn_write;
String file_name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.TextView01);
btn_readWebpage = (Button) findViewById(R.id.readWebpage);
}
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));
file_name = ReadWebpageAsyncTaskActivity.this.getFilesDir()
.getAbsolutePath() + "/text.txt";
// file_name = Environment.getExternalStorageDirectory(
// "/textFile.txt";
File file = new File(file_name);
boolean exist = file.createNewFile();
if (!exist)
System.out.println("File already exists.");
FileWriter fstream = new FileWriter(file_name);
BufferedWriter out = new BufferedWriter(fstream);
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
out.write(s);
}
out.close();
System.out.println("File created successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
Log.d("\nResponse========: ", "String" + response);
return response;
}
@Override
protected void onPostExecute(String response) {
// textView.setText(response);
String result = "";
String strLine;
File file = new File(file_name);
FileInputStream fin;
try {
fin = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
while ((strLine = br.readLine()) != null) {
result += strLine;
}
textView.setText(result);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void readWebpage(View view) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.vogella.de" });
}
}
/***************************main.xml***************************/
package com.asynctask.android;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
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.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ReadWebpageAsyncTaskActivity extends Activity {
private TextView textView;
Button btn_readWebpage;
Button btn_write;
String file_name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.TextView01);
btn_readWebpage = (Button) findViewById(R.id.readWebpage);
}
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));
file_name = ReadWebpageAsyncTaskActivity.this.getFilesDir()
.getAbsolutePath() + "/text.txt";
// file_name = Environment.getExternalStorageDirectory(
// "/textFile.txt";
File file = new File(file_name);
boolean exist = file.createNewFile();
if (!exist)
System.out.println("File already exists.");
FileWriter fstream = new FileWriter(file_name);
BufferedWriter out = new BufferedWriter(fstream);
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
out.write(s);
}
out.close();
System.out.println("File created successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
Log.d("\nResponse========: ", "String" + response);
return response;
}
@Override
protected void onPostExecute(String response) {
// textView.setText(response);
String result = "";
String strLine;
File file = new File(file_name);
FileInputStream fin;
try {
fin = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
while ((strLine = br.readLine()) != null) {
result += strLine;
}
textView.setText(result);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void readWebpage(View view) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.vogella.de" });
}
}
/***************************main.xml***************************/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/readWebpage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load Webpage"
android:onClick="readWebpage">
</Button>
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Example Text" >
</TextView>
</LinearLayout>