How to view your web-page source code in android device
First create a project with the name WebsiteAnalysisTools and have a main activity with following code:
package com.clearpath.websiteanalysistools;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText url_text;
TextView textView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
url_text = (EditText) findViewById(R.id.address);
textView = (TextView) findViewById(R.id.tv);
}
public void myButtonClickHandler(View view) {
new GrabURL().execute();
}
private class GrabURL extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String response_str;
private String Error = null;
private ProgressDialog Dialog = new ProgressDialog(MainActivity.this);
protected void onPreExecute() {
Dialog.setMessage("Processing..");
Dialog.show();
}
protected Void doInBackground(String... urls) {
try {
HttpGet request = new HttpGet(url_text.getText().toString());
// Get the response
ResponseHandler<String> responseHandler = new BasicResponseHandler();
response_str = Client.execute(request, responseHandler);
Log.d("HTML:", response_str);
} catch (ClientProtocolException e) {
Error = e.getMessage();
cancel(true);
} catch (IOException e) {
Error = e.getMessage();
cancel(true);
}
return null;
}
protected void onPostExecute(Void unused) {
Dialog.dismiss();
textView.setText(response_str);
if (Error != null) {
Toast.makeText(MainActivity.this, Error, Toast.LENGTH_LONG).show();
}
}
}
}
The main layout file is presented here which is activity_main.xml
<?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">
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/address"
android:hint="http://www.google.com"
android:singleLine="true">
</EditText>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read Webpage"
android:id="@+id/ReadWebPage"
android:onClick="myButtonClickHandler">
</Button>
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tv"
android:scrollbars="vertical">
</TextView>
</ScrollView>
</LinearLayout>
No comments:
Post a Comment