前者:
// 創建子線程
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();
}
};
};
http://www.cnblogs.com/yourancao520/archive/2012/06/25/2561367.html這裡有篇文章和你類似,可以看看,他的是將
Bitmap bitmap = BitmapFactory.decodeStream(in)改成了Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);