【实战】android网页源代码查看器
程序员文章站
2022-06-21 10:46:11
...
点击查看源代码,可以查看该网页的源代码
一、文件结构
MainActivity.java:
package com.example.viewpagesource;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import com.example.viewpagesource.utils.StreamTool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Menu;
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 implements View.OnClickListener {
protected static final int SUCCESS = 0;
protected static final int ERROR = 1;
protected static final int NETWORK_ERROR = 2;
private Button btn;
private EditText ed_path;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
ed_path=(EditText) findViewById(R.id.ed_path);
tv=(TextView) findViewById(R.id.tv);
}
//定义一个秘书
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SUCCESS:
String value=(String) msg.obj;
tv.setText(value);
break;
case ERROR:
System.out.println("ERROR");
Toast.makeText(MainActivity.this, "错误发生了", 0).show();
break;
case NETWORK_ERROR:
System.out.println("NETWORK_ERROR");
Toast.makeText(MainActivity.this, "错误发生了", 0).show();
break;
default:
break;
}
};
};
String path;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
path = ed_path.getText().toString().trim();
if(TextUtils.isEmpty(path)){
Toast.makeText(this, "路径有错误", 0).show();
return;
}
//连接网络,要启动一个新的线程去,干耗时的事情
new Thread(){
public void run() {
try {
URL url=new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置连接超时为5秒钟
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
String contentType = conn.getContentType();
int code = conn.getResponseCode();
if(code==200){
InputStream in = conn.getInputStream();
//这里的data是从服务器返回的
String data=StreamTool.decodeStream(in);
//展示在屏幕上
Message msg=Message.obtain();
msg.what=SUCCESS;
msg.obj=data;
handler.sendMessage(msg);
}else{
Message msg=Message.obtain();
msg.what=ERROR;
handler.sendMessage(msg);
}
} catch (Exception e) {
// TODO Auto-generated catch block
Message msg=Message.obtain();
msg.what=NETWORK_ERROR;
handler.sendMessage(msg);
e.printStackTrace();
}
};
}.start();
}
}
StreamTool.java
package com.example.viewpagesource.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class StreamTool {
public static String decodeStream(InputStream in) throws IOException {
// TODO Auto-generated method stub
//底层流
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int len=0;
byte[] buf=new byte[1024];
while((len=in.read(buf))>0){
baos.write(buf,0,len);
}
String data = baos.toString();
return data;
}
}
activity_main.xml:
<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" >
<EditText
android:id="@+id/ed_path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="http://www.baidu.com" />
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="查看网页源代码"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tv" />
</ScrollView>
</LinearLayout>
AndroidMainfest.xml
<uses-permission android:name="android.permission.INTERNET"/>
上一篇: 获取DOM元素样式
下一篇: 2021-05-26
推荐阅读