Android 中HttpURLConnection与HttpClient使用的简单实例
更新时间:2013年10月15日 16:52:20 作者:
这篇文章介绍了Android 中HttpURLConnection与HttpClient使用的简单实例,有需要的朋友可以参考一下
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;
}
}
相关文章
基于barcodescanner实现Android二维码扫描功能
这篇文章主要为大家详细介绍了基于barcodescanner实现Android二维码扫描功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-07-07
使用Win10+Android+夜神安卓模拟器,搭建ReactNative开发环境
今天小编就为大家分享一篇关于使用Win10+Android+夜神安卓模拟器,搭建ReactNative开发环境,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2018-10-10
Flutter 使用cached_image_network优化图片加载体验
在 Flutter 中,cached_image_network 即提供了缓存网络图片功能,同时还提供了丰富的加载过程指示。本文就来看下cached_image_network的具体使用2021-05-05
android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法
这篇文章介绍了android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法,有需要的朋友可以参考一下2013-09-09


最新评论