android中测试 跑得了 加点分可以继续追问我哦 呵呵~~~~
public static String getHtml( String urlpath,String encoding) throws Exception {
URL url = new URL(urlpath);
//实例化一个HTTP连接对象conn
HttpURLConnection conn = (HttpURLConnection)url。
openConnection();
//定义请求方式为GET,其中GET的格式需要注意
tRequestMethod("GET");
//定义请求时间,在ANDROID中最好是不好超过10秒。否则将被系统回收。
tConnectTimeout(6*1000);
if( tResponseCode()== 200){
InputStream inStream = tInputStream();
byte[] data = readStream(inStream);
return new String(data,encoding);
}
return null;
}
public static byte[] readStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len= -1;
//将输入流不断的读,并放到缓冲区中去。
直到读完
while((len= ad(buffer))!=-1){
//将缓冲区的数据不断的写到内存中去。
outStream。write(buffer, 0, len);
}
ose();
ose();
return ByteArray();。
全部