package com.cook.schedule;import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;public class GetWebThread2 extends Thread
{
@Override
public void run()
{
String urlName="http://www.baidu.com";
try {
URL url=new URL(urlName);
URLConnection connection=url.openConnection();
connection.addRequestProperty("Accept-Charset", "UTF-8");
connection.connect();
byte[] data = readInputStream(connection.getInputStream());
String str=new String(data,"UTF-8");
System.out.println(str);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private byte[] readInputStream(InputStream inStream)
throws IOException {
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inStream.read(buffer)) != -1) {
byteOutputStream.write(buffer, 0, len);
}
inStream.close();
return byteOutputStream.toByteArray();
}
}再AVD上测试,打印台上只有一半页面:
html
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;public class GetWebThread2 extends Thread
{
@Override
public void run()
{
String urlName="http://www.baidu.com";
try {
URL url=new URL(urlName);
URLConnection connection=url.openConnection();
connection.addRequestProperty("Accept-Charset", "UTF-8");
connection.connect();
byte[] data = readInputStream(connection.getInputStream());
String str=new String(data,"UTF-8");
System.out.println(str);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private byte[] readInputStream(InputStream inStream)
throws IOException {
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inStream.read(buffer)) != -1) {
byteOutputStream.write(buffer, 0, len);
}
inStream.close();
return byteOutputStream.toByteArray();
}
}再AVD上测试,打印台上只有一半页面:
html
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货