网页源代码查看器
目录
一、项目目录结构
二、类:MainActivity
package com.example.day0801;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.HttpConnection;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText ed_url;
private Button bt_look;
private TextView tv_url;
//创建一个Handler,在主线程的消息处理
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
tv_url.setText(msg.obj.toString());
//Toast.makeText(MainActivity.this, "-----handleMessage----", Toast.LENGTH_SHORT).show();
System.out.println("--yuansheng--msg.obj.toString():"+msg.obj.toString());
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_url = (EditText)findViewById(R.id.ed_url);
bt_look = (Button)findViewById(R.id.bt_look);
tv_url = (TextView)findViewById(R.id.tv_url);
//ed_url.setText("http://10.0.2.3:8080");
//测试ok,但是需要将模拟器的通过adb shell的【setprop net.dns1 192.168.1.1】命令ip设置为192.168.1.1
ed_url.setText("https://www.baidu.com");
bt_look.setOnClickListener(new MyOnClickListener());
}
class MyOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
// Android 4.0 之后联网不能在主线程中请求HTTP请求,只能在子线程联网
new Thread(){
public void run() {
try {
//!!!!!!!Toast消息不能在子线程中出现
//Toast.makeText(MainActivity.this, "-----点击----", Toast.LENGTH_SHORT).show();
//获取url
String urlstr = ed_url.getText().toString().trim();
//Toast.makeText(MainActivity.this, urlstr, Toast.LENGTH_SHORT).show();
URL url = new URL(urlstr);
//URL url = new URL("http://10.0.2.3:8080");
HttpURLConnection openConnection = (HttpURLConnection)url.openConnection();
openConnection.setRequestMethod("GET");
openConnection.setConnectTimeout(10000);
//获取响应码
int responseCode = openConnection.getResponseCode();
//Toast.makeText(MainActivity.this, "responseCode"+responseCode, Toast.LENGTH_SHORT).show();
System.out.println("--yuansheng--responseCode:"+responseCode);
if(responseCode == 200) {
InputStream inputStream = openConnection.getInputStream();
String stringformStream = Utils.getStringformStream(inputStream);
System.out.println("--yuansheng--stringformStream:"+stringformStream);
//UI操作必须在主线程,所以,主线程也叫UI线程
//tv_url.setText(stringformStream);
Message message = new Message();
message.obj = stringformStream;
handler.sendMessage(message);
}
//Toast.makeText(MainActivity.this, "-----点击完----", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("--yuansheng--Exception:"+e);
}
}
}.start();
}
}
}
三、类:Utils
package com.example.day0801;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class Utils {
public static String getStringformStream(InputStream inputStream) {
int len = -1;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] byteArray = new byte[1024];
try {
while((len = inputStream.read(byteArray)) != -1) {
byteArrayOutputStream.write(byteArray, 0, len);
}
inputStream.close();
byte[] array = byteArrayOutputStream.toByteArray();
return new String(array);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
四、activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.day0801.MainActivity" >
<EditText
android:id="@+id/ed_url"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="请输入url!"
android:inputType="textUri"
/>
<Button
android:id="@+id/bt_look"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/ed_url"
android:text="确定"
/>
<ScrollView
android:layout_below="@id/bt_look"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<TextView
android:id="@+id/tv_url"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#FFB6C1"
/>
</ScrollView>
</RelativeLayout>
五、AndroidManifest.xml(加入网络权限)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.day0801"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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>
</manifest>