httpCilent请求网络 及 StreamUtils 工具类转换
2016-08-22 11:50
302 查看
ry { // 创建请求的客户端对象 HttpClient httpClient = new DefaultHttpClient(); // 定义GET请求对象 HttpGet httpGet = new HttpGet(path); // 执行get请求--获取到响应的对象 HttpResponse httpResponse = httpClient.execute(httpGet); // 获取状态行对象 StatusLine statusLine = httpResponse.getStatusLine(); // 获取状态码 int statusCode = statusLine.getStatusCode(); if (statusCode == 200) { // 获取实体对象 HttpEntity entity = httpResponse.getEntity(); // 获取实体内容 InputStream inputStream = entity.getContent(); // 将流转换成字符串 String json = StreamUtils.parseSteam(inputStream); // 发送给主线程 handler.obtainMessage(SUSCCESS, json).sendToTarget(); } } catch (Exception e) { e.printStackTrace(); } public class StreamUtils { public static String parseSteam(InputStream inputStream) { try { //定义一个字节数组输出流 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); //定义一个字节数组 byte[] buffer = new byte[1024]; //定义初始长度 int len = 0; while((len = inputStream.read(buffer))!=-1){ //将读的内容写到字节数组输出流中 outputStream.write(buffer, 0, len); } //将字节输出流转化成字符串 return outputStream.toString("utf-8"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }
相关文章推荐
- Android-async-http 网络请求框架(工具类)
- AS3.0 网络加载Http请求工具类
- Android网络请求,Get方式请求,HttpURLConnection请求工具类
- 利用线程池实现Android客户端的http网络数据请求工具类
- HttpURLConnection网络请求工具类
- HttpUtils 用于进行网络请求的工具类
- 利用线程池实现Android客户端的http网络数据请求工具类
- HttpClient与HttpURLConnection网络请求工具类
- Android 原生HttpURLConnection网络请求工具类(get post)
- 网络请求工具类HttpUtil
- Android常用工具类之Http网络请求的工具类
- HttpURLConnection 单独封装网络请求工具类
- Android HttpUtils网络请求工具类的实现
- HttpUrlConnection 网络请求工具类
- 网络请求应该是HttpUtil 工具类
- Andriod通用封装工具类--HttpCilentHelper
- Android-网络请求数据使用apache httpclient v4
- android网络数据请求之HttpClient
- iOS网络编程-ASIHTTPRequest框架同步请求
- httpClient 用于发送HTTP请求的工具类