Android网络请求
2016-08-05 18:14
281 查看
简单的HttpURLConnection网络请求数据
将我们请求的
private void requestGson() { progressDialog.show(); new Thread() { public void run() { try { URL url = new URL(address + index); HttpURLConnection openConnection = (HttpURLConnection) url .openConnection(); openConnection.setConnectTimeout(5000); int responseCode = openConnection.getResponseCode(); if (responseCode == 200) { // InputStream inputStream = openConnection .getInputStream(); String json = StreamUtils.parseStream(inputStream); // 解析json Gson gson = new Gson(); WeiInfo weiInfo = gson.fromJson(json, WeiInfo.class); handler.obtainMessage(0, weiInfo).sendToTarget(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); }empty
将我们请求的
public class StreamUtils { public static String parseStream(InputStream inputStream) { try { ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inputStream.read(buffer)) != -1) { arrayOutputStream.write(buffer, 0, len); } return arrayOutputStream.toString(); } catch (IOException e) { e.printStackTrace(); } return null; } }
相关文章推荐
- List of HTTP header fields
- cocos quickhttp文件下载测试
- 深入了解HTTP和Socket在实时性Web上的实践
- Android网络请求HttpUtil封装工具类
- Linux网络编程【1】
- 《TCP/IP详解 卷1:协议》读书笔记 第2章 链路层
- 配置wincapWinPcap 4.0.1在线中文手册:http://www.ferrisxu.com/WinPcap/html/index.html 本文以Visual Studio 2013和
- 实例讲解虚拟机3种网络模式(桥接、nat、Host-only)
- [置顶] Okhttp 向服务器发送请求(请求头,表单,post json数据)
- libevent evhttp学习——http客户端
- 使用RxJava+OKhttp3进行图片下载
- 浅析Node.js实现HTTP文件下载
- 【HTTP】Wireshark过滤规则
- angularjs $http 服务
- python 写的http后台弱口令爆破工具
- TCP服务端收到syn但是不回复syn ack问题分析
- 线程之间的通信实现wc -w
- HttpURLConnection获取图片数据流
- heartbeat httpd nfs 实现高可用web集群
- 【HTTP】WireShark中获取Content-Encoding: gzip时的响应内容