Android 中HttpURLConnection与HttpClient使用的简单实例
程序员文章站
2022-10-09 15:04:52
1:httphelper.java
复制代码 代码如下:public class httphelper { //1:标准的java接口...
1:httphelper.java
复制代码 代码如下:
public class httphelper {
//1:标准的java接口
public static string getstringfromnet1(string param){
string result="";
try{
url url=new url(param);
httpurlconnection conn=(httpurlconnection)url.openconnection();
if(conn.getresponsecode()==httpurlconnection.http_ok){
inputstream is=conn.getinputstream();
byte[]data=new byte[1024];
int len=is.read(data);
result=new string(data,0,len);
is.close();
conn.disconnect();
}
}catch(exception e){
e.printstacktrace();
}
return result;
}
//2:apache接口
public static string getstringfromnet2(string param){
string result="";
try{
httpclient client=new defaulthttpclient();
httpget get=new httpget(param);
httpresponse response=client.execute(get);
if(response.getstatusline().getstatuscode()==httpstatus.sc_ok){
result=entityutils.tostring(response.getentity());
}
}catch(exception e){
e.printstacktrace();
}
return result;
}
}
推荐阅读