前者:
// 创建子线程
new Thread() {
public void run() {
try {
// 获取源码路径
String path = et_enter.getText().toString().trim();
// 将路径转换为URL
URL url = new URL(path);
// 利用URL对象得到httpURLconnection对象 用于发送或接收数据
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
// 发送get请求,以得到网页源代码
conn.setRequestMethod("GET");// 必须大写
// 设置请求超时时间
conn.setConnectTimeout(5000);
// 获取服务器返回的状态码
int code = conn.getResponseCode();
// 判断是否请求成功
if (code == 200) {
InputStream in = conn.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
String content = new String(baos.toByteArray());
// 创建message对象用于存取content
Message msg = new Message();
msg.obj = content;
// 调用sendMessage方法发送消息
hander.sendMessage(msg);
// tv_display.setText(content);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
};
后者:
// 创建子线程
new Thread(){
public void run() {
try {
// 获取源码路径
String path = et_enter.getText().toString().trim();
// 将路径转换为URL
URL url = new URL(path);
// 利用URL对象得到httpURLconnection对象 用于发送或接收数据
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
// 发送get请求,以得到网页源代码
conn.setRequestMethod("GET");// 必须大写
// 设置请求超时时间
conn.setConnectTimeout(5000);
// 获取服务器返回的状态码
int code = conn.getResponseCode();
// 判断是否请求成功
if (code == 200) {
InputStream in = conn.getInputStream();
//通过BitmapFactory获取bitmap
Bitmap bitmap = BitmapFactory.decodeStream(in);
// 创建message对象用于存取content
Message msg = Message.obtain();
msg.obj = bitmap;
// 调用sendMessage方法发送消息
hander.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
};
};