Android 中HttpURLConnection与HttpClient使用的简单实例
2013-10-15 16:52
1016 查看
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;
}
}
您可能感兴趣的文章:
- Android使用URLConnection提交请求的实现
- Android HttpURLConnection.getResponseCode()错误解决方法
- Android中HttpURLConnection与HttpClient的使用与封装
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- Android通过HttpURLConnection和HttpClient接口实现网络编程
- Golang+Android基于HttpURLConnection实现的文件上传功能示例
- Android开发使用HttpURLConnection进行网络编程详解【附源码下载】
- android 网络编程之网络通信几种方式实例分享
- Android网络编程之UDP通信模型实例
- Android开发使用URLConnection进行网络编程详解
相关文章推荐
- 【转】Android HttpClient && HttpPost简单使用
- Android HttpClient && HttpPost简单使用
- 12 Android AsyncHttpClient 的简单使用
- Android HttpClient && HttpPost简单使用
- Android 中HttpURLConnection与HttpClient的简单使用
- Android网络(4):HttpClient必经之路----使用线程安全的单例模式HttpClient,及HttpClient和Application的融合
- Android 5.0之后因为Google废弃了相关HttpClient的代码,所以无法使用HttpClient
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- Android HTTP实例 使用GET方法和POST方法发送请求
- 简单使用URLConnection、HttpURLConnection和HttpClient访问网络资源
- android上使用Soap协议的简单实例
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android Service使用方法--简单音乐播放实例
- Android访问网络,使用HttpURLConnection还是HttpClient?
- httpclient使用实例
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- WCF服务端与使用HttpURLConnection的Android客户端简单示例
- 使用libevent实现最简单的android http-server
- Android AnalogClock简单使用方法实例
- Android 异步请求类库Async_Http_Client的使用方法