安卓中像解析一些JSON,XML文件用到的请求网络的工具类,很方便
<span style="font-size:18px;">package com.example.login;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class AA {
public static String requestHttp(String path)
{
ByteArrayOutputStream bo=new ByteArrayOutputStream();
try
{
URL url=new URL(path);
HttpURLConnection con=(HttpURLConnection) url.openConnection();
con.setConnectTimeout(5000);
con.setDoInput(true);
con.connect();
if(con.getResponseCode()==200)
{
InputStream in = con.getInputStream();
int count=0;
byte b[]=new byte[1024];
while((count=in.read(b))!=-1)
{
bo.write(b, 0, count);
bo.flush();
}
}
return bo.toString();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
</span>