protected void setwebsit(String urlstr) {
try {
// 首先创建对象
URL url = new URL(urlstr);
// 通过URL打开连接
HttpURLConnection open = (HttpURLConnection) url.openConnection();
// 设置连接超时的时间
// open.setConnectTimeout(1000);
// 设置读取超时时间
// open.setReadTimeout(1000);
open.connect();
// 获取响应码
int res = open.getResponseCode();
if (200 == res) {
// 获取输入流
InputStream is = open.getInputStream();
byte[] buf = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// 强内容读取到
int len = -1;
while ((len = is.read(buf)) != -1) {
bos.write(buf, 0, len);
}
// 关闭流
is.close();
final String text = bos.toString("utf-8");
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(text);
}
});
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
// 首先创建对象
URL url = new URL(urlstr);
// 通过URL打开连接
HttpURLConnection open = (HttpURLConnection) url.openConnection();
// 设置连接超时的时间
// open.setConnectTimeout(1000);
// 设置读取超时时间
// open.setReadTimeout(1000);
open.connect();
// 获取响应码
int res = open.getResponseCode();
if (200 == res) {
// 获取输入流
InputStream is = open.getInputStream();
byte[] buf = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// 强内容读取到
int len = -1;
while ((len = is.read(buf)) != -1) {
bos.write(buf, 0, len);
}
// 关闭流
is.close();
final String text = bos.toString("utf-8");
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(text);
}
});
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}