Android 中HttpURLConnection与HttpClient使用的简单实例_安卓软件开发教程-查字典教程网
Android 中HttpURLConnection与HttpClient使用的简单实例
Android 中HttpURLConnection与HttpClient使用的简单实例
发布时间:2016-12-28 来源:查字典编辑
摘要:1:HttpHelper.java复制代码代码如下:publicclassHttpHelper{//1:标准的Java接口publicsta...

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;

}

}

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新安卓软件开发学习
    热门安卓软件开发学习
    编程开发子分类