网络HTML查看器
网络HTML查看器技术点 a) Message msg = Message.obtain();
//推荐写法,防止过多的Message,这是一个Message池。
b) 流转成文本-常用的操作-utils
ByteArrayOutputStream不要close()里面是个空实现
http://www.google.fr/ 或者http://www.google.ir (爱尔兰)
内容不会被阉割
c) jsp采用utf-8不会乱码 ,
jsp采用gbk,出现乱码,android中采用的全部是utf-8的(乱码说明两端的编码解码不一致)
|
核心代码: package cn.zengfansheng.htmlviewer;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
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;
}
} |
结果:
|
问题1:乱码问题 jsp采用gbk编码,而android默认是以utf-8查看,显示乱码。而浏览器是在windows下,默认编码是gbk显示正常。
解决: a) 将jsp页面以utf-8编码 b) android解码时指定解码格式以gbk解码 String content = new String(byteArray, "gbk"); // Android默认采用utf-8编码,而浏览器在windows下默认采用gbk编码
jsp以gbk编码,而浏览器显示正常,因为windows平台默认gbk:
|