HttpURLConnection的总结
来源:互联网 发布:js test函数 编辑:程序博客网 时间:2024/06/07 04:45
Android6.0(API:23)的发布,带来了许多的特性,比如近期讨论较多的运行时授权,另外,在使用6.0的API的时候,也发现HttpClient也成为了过去时。虽说在2.3后,HttpURLConnection就已经成为主流,但还是有不少人使用HttpClient,因此,这里将HttpURLConnection做个简单的介绍:
一、HttpURLConnection的API
- 字段
- chunkLength:使用存储块编码流模式进行输出时的存储块长度。
- fixedContentLength 使用固定长度流模式时的固定内容长度。
- 其它的主要是一些状态码。(典型的为200:HTTP_OK,具体的可以参考一下这篇资料:HttpURLConnection的API)
- 构造方法
- HttpURLConnection(URL u);
- 常用方法
- disconnect(); 关闭连接
- getResponseCode();获取HTTP响应消息
- 其它方法可以根据需要选择
二、相关联的类及方法
- 在安卓中,使用HttpURLConnection显然要进行权限设置:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
其中第一条,是获取网络状态的权限,第二条是连接网络的权限 - URL
HttpURLConnection的实例几乎都是由url的openConnection获得的。它表示到 URL 所引用的远程对象的连接,当然,它返回的是一个 URLConnection 对象,因此需要强制转换。 - 从网络总是为了获取数据的,因此,最为重要的是IO流的处理,几乎每个常用的流都有可能用到。
三、网络工具类中重用的几个方法
- 一般先判断网络是否可用
public static boolean isNetWorkConnection(Context context) {// 判断网络状态 ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); if (info != null) { // 返回网络的连接状态 return info.isConnected(); } return false; }
2.获得String字符串
// Android中常用来从网络获取Json字符串 public static String getJsonString(String path) { HttpURLConnection connection = null; StringBuffer sb = null; try { URL url = new URL(path); connection = (HttpURLConnection) url.openConnection(); int code = connection.getResponseCode(); if (code == 200) { // 得到InputStream流后,通过转换流,最后得到缓冲字符输入流 BufferedReader br = new BufferedReader(new InputStreamReader( connection.getInputStream(), "utf-8")); String line; sb = new StringBuffer(); while ((line = br.readLine()) != null) { sb.append(line); } if (br != null) { br.close(); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(fs!=null){ sb.close(); } if (connection != null) { connection.disconnect(); } } return sb.toString(); }
3.获得byte[]数组
// 一般从网络获取的是图片、音乐链接的资源使用public static byte[] getBytesResult(String path) { HttpURLConnection connection = null; // 因为要返回字符数组,使用ByteArrayOutputStream流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { URL url = new URL(path); connection = (HttpURLConnection) url.openConnection(); int code = connection.getResponseCode(); if (code == 200) { InputStream is = connection.getInputStream(); int len = 0; byte[] bytes = new byte[512]; // 流之间的转换 while ((len = is.read(bytes)) != -1) { baos.write(bytes, 0, len); baos.flush(); } is.close(); baos.close(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } } // baos中有方法直接将流转为字符数组 return baos.toByteArray(); }
- 一般得到的字符流需要进一步的操作,比如
- 得到Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(byte[] data, int offset,int length);//offset一般为0,length 可以由data.length得到。
0 0
- Android的HttpURLConnection总结
- HttpURLConnection的总结
- HttpURLConnection的get请求总结
- HttpURLConnection的post请求总结
- HttpURLConnection总结
- 项目总结-HttpURLConnection的disconnect的问题
- HttpURLConnection 分析总结
- HttpUrlConnection使用总结
- Java HttpURLConnection 总结
- HttpURLConnection--HttpURLConnection的基本用法
- URLConnection类,HttpURLConnection类的使用和总结
- HttpURLConnection的GET请求POST请求小总结
- Android HttpClient HttpUrlConnection用法总结
- HttpURLConnection--HttpURLConnection的Get请求方式
- HttpURLConnection--HttpURLConnection的Post请求方式
- HttpURLConnection 的 POST 方法
- HttpURLConnection的使用
- HttpURLConnection类的用法
- Sql Server 分页查询
- background-position 用法详细介绍
- 【Python】Learn Python the hard way, ex39 字典操作
- linux中shell变量$#,$@,$0,$1,$2的含义解释
- Clojure 文档翻译--概述及类库导入
- HttpURLConnection的总结
- C++与JAVA的异同
- 流程控制语言、、循环控制语言
- DBA的特质
- 黑马程序员——集合
- Python 简介
- 连续子数组最大和问题
- 工作生活阶段整理序言
- 友盟社会化分享的坑——微信好友/朋友圈分享跳转不到内容编辑界面