Android 网络编程相关
2016-05-18 10:47
225 查看
1.HttpClient
1.1 Get
[b]2.HttpURLConnection[/b]
1)创建一个URL对象
URL url = new URL(http://www.baidu.com);
2)利用HttpURLConnection对象从网络中获取网页数据
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
3)设置连接超时
conn.setConnectTimeout(6*1000);
4)对响应码进行判断
if (conn.getResponseCode() != 200) //从Internet获取网页,发送请求,将网页以流的形式读回来
throw new RuntimeException("请求url失败");
5)得到网络返回的输入流
InputStream is = conn.getInputStream();
6)String result = readData(is, "GBK"); //文件流输入出文件用outStream.write
7)conn.disconnect();
1.1 Get
[b]2.HttpURLConnection[/b]
1)创建一个URL对象
URL url = new URL(http://www.baidu.com);
2)利用HttpURLConnection对象从网络中获取网页数据
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
3)设置连接超时
conn.setConnectTimeout(6*1000);
4)对响应码进行判断
if (conn.getResponseCode() != 200) //从Internet获取网页,发送请求,将网页以流的形式读回来
throw new RuntimeException("请求url失败");
5)得到网络返回的输入流
InputStream is = conn.getInputStream();
6)String result = readData(is, "GBK"); //文件流输入出文件用outStream.write
7)conn.disconnect();
相关文章推荐
- HttpClient使用详解
- 深度学习与计算机视觉系列(6)_神经网络结构与神经元激励函数
- 判断网络状态
- tcpdump常用参数说明
- 一点一点学ASP.NET之基础概念——HttpHandler
- 【转】Android网络编程之Socket&Http
- iOS网络4——Reachability检测网络状态
- xmlhttprequest报没有权限
- OKHttp使用简介--使用
- Exception in thread "http-bio-8037-exec-45" java.lang.OutOfMemoryError: PermGen space
- 使用NAT模式对外提供WEB服务的网关的网络过程
- TCP/IP,http,socket,长连接,短连接 总结
- 网络服务调用总结
- Android从网络获取北京时间以及动态的监听时间
- Serlvet 处理http请求并保持长连接
- java 常见几种发送http请求案例
- 将网络上的图片缓存到本地指定目录文件夹下方式。
- ImageLoader加载https图片
- 高效地配置OkHttp
- (Thread)线程-线程池-HttpUrlConnection-压缩Bitmap的小整合