网页源码查看器
核心代码如下:
import cn.zengfansheng.html.utils.StreamUtils;
/**
* 网络html查看器
* 1、布局
* 2、获取路径
* 3、封装成url,获取连接,发送请求
* 4、将服务器返回的输入流转成文本(常见的操作,用一个utils)
* 5、显示在相应的控件上面
*
*/
public class MainActivity extends Activity {
protected static final int ERRORCODE = 1;
protected static final int INTERNALERROR = 2;
protected static final int CONTENT = 3;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case ERRORCODE:
Toast.makeText(MainActivity.this, "服务器返回错误", 0).show();
break;
case INTERNALERROR:
Toast.makeText(MainActivity.this, "获取数据失败", 0).show();
break;
case CONTENT:
String text = (String) msg.obj;
tv_content.setText(text);
break;
}
}
};
private EditText et_path;
private TextView tv_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) this.findViewById(R.id.et_path);
tv_content = (TextView) this.findViewById(R.id.tv_content);
}
/**
* 获取html内容
* @param view
*/
public void getHtmlContent(View view){
// 1、获取用户输入的html路径
final String path = et_path.getText().toString().trim();
// 2、路径path判空
if (TextUtils.isEmpty(path)) {
Toast.makeText(this, "路径不能为空!!!", 0).show();
} else {
// 3、path不为空,开启新的线程去获取html资源
new Thread() {
@Override
public void run() {
try {
// 3-1 封装成url对象
URL url = new URL(path);
// 3-2 获取http连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 3-3 设置请求参数
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);// 5秒连接超时
conn.setReadTimeout(5000);// 5秒读取超时
// 3-4获取服务器返回码
int code = conn.getResponseCode();
if (code == 200) {
InputStream in = conn.getInputStream();
// 3-5将in转成文本
String text = StreamUtils.streamToText(in);
// 3-6发送消息通知主线程更新ui
Message msg = Message.obtain();
msg.what = CONTENT;
msg.obj = text;
handler.sendMessage(msg);
} else {
Message msg = Message.obtain();// 避免过多的Message对象
msg.what = ERRORCODE;
handler.sendMessage(msg);
}
} catch (MalformedURLException e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what = INTERNALERROR;
handler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what = INTERNALERROR;
handler.sendMessage(msg);
}
}
}.start();
}
}
}
//流数据转换成文本数据的方法
StreamUtils.java
package cn.zengfansheng.html.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class StreamUtils {
/**
* 把一个输入流里面的内容 转化成文本 字符串
* @param in 输入流
* @return 返回文本String
* @throws IOException
*/
public static String streamToText(InputStream in) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte[] buf = new byte[1024];
while ((len = in.read(buf)) != -1) {
baos.write(buf, 0, len);
}
byte[] byteArray = baos.toByteArray();
String content = new String(byteArray, "gbk");
// Android默认采用utf-8编码,而浏览器在windows下默认采用gbk编码
return content;
}
注意事项
- 由于我们最后是把网页源码转换成了文本数据,那么就要考虑一个经典问题了:”中文乱码”问题,由于android的默认编码是”UTF-8”,而网页有可能是采用的别的编码集,例如”GBk”等等什么?怎么解决这个问题了,也好解决,就是让服务器和android客户端采用相同的编码集就行,只要编码统一了就不会出现这个乱码的问题
用到的重要api
- ByteArrayOutputStream()//把数据写入到内存中,然后再把它转换成字符串