Android使用AsynchronousHttpClient
2015-11-09 10:07
495 查看
Asynchronous Http Client是android中非常好的异步请求工具
除了异步之外还有很多封装比如json的处理,cookie的处理
引用
Persistent Cookie Storage with PersistentCookieStore
This library also includes a PersistentCookieStore which is an implementation of the Apache HttpClient CookieStore interface that automatically saves cookies to SharedPreferences storage on the Android device.
This is extremely useful if you want to use cookies to manage authentication sessions, since the user will remain logged in even after closing and re-opening your app.
First, create an instance of AsyncHttpClient:
AsyncHttpClient myClient = new AsyncHttpClient();
Now set this client’s cookie store to be a new instance of PersistentCookieStore, constructed with an activity or application context (usually this will suffice):
Java代码
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
myClient.setCookieStore(myCookieStore);
Any cookies received from servers will now be stored in the persistent cookie store.
To add your own cookies to the store, simply construct a new cookie and call addCookie:
Java代码
BasicClientCookie newCookie = new BasicClientCookie("cookiesare", "awesome");
newCookie.setVersion(1);
newCookie.setDomain("mydomain.com");
newCookie.setPath("/");
myCookieStore.addCookie(newCookie);
See the PersistentCookieStore Javadoc for more information.
开篇边有一句话描述“utomatically saves cookies to SharedPreferences storage ”
自动保存cookie到SharedPreferences 中
需要注意的一点是下面设置cookie的代码
Java代码
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
myClient.setCookieStore(myCookieStore);
必须在client发起请求之前执行,然后再去执行client的get或者post请求。做好设置之后,它在请求之后才会把返回http的head中获取cookie保存。
否则是无法保存的。
除了异步之外还有很多封装比如json的处理,cookie的处理
引用
Persistent Cookie Storage with PersistentCookieStore
This library also includes a PersistentCookieStore which is an implementation of the Apache HttpClient CookieStore interface that automatically saves cookies to SharedPreferences storage on the Android device.
This is extremely useful if you want to use cookies to manage authentication sessions, since the user will remain logged in even after closing and re-opening your app.
First, create an instance of AsyncHttpClient:
AsyncHttpClient myClient = new AsyncHttpClient();
Now set this client’s cookie store to be a new instance of PersistentCookieStore, constructed with an activity or application context (usually this will suffice):
Java代码
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
myClient.setCookieStore(myCookieStore);
Any cookies received from servers will now be stored in the persistent cookie store.
To add your own cookies to the store, simply construct a new cookie and call addCookie:
Java代码
BasicClientCookie newCookie = new BasicClientCookie("cookiesare", "awesome");
newCookie.setVersion(1);
newCookie.setDomain("mydomain.com");
newCookie.setPath("/");
myCookieStore.addCookie(newCookie);
See the PersistentCookieStore Javadoc for more information.
开篇边有一句话描述“utomatically saves cookies to SharedPreferences storage ”
自动保存cookie到SharedPreferences 中
需要注意的一点是下面设置cookie的代码
Java代码
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
myClient.setCookieStore(myCookieStore);
必须在client发起请求之前执行,然后再去执行client的get或者post请求。做好设置之后,它在请求之后才会把返回http的head中获取cookie保存。
否则是无法保存的。
相关文章推荐
- loadrunner处理HTTP重定向请求
- 【读书笔记】iOS网络-保护网络传输
- 【读书笔记】iOS网络-保护网络传输
- 让浏览器不再显示 https 页面中的 http 请求警报
- C++ 用libcurl库进行http 网络通讯编程
- NodeJS http 模块
- HTTP 长连接和短连接
- 360云后台(使用HTTP Cache服务器)
- ajax网络请求时候的data参数应该用JSON.stringify?
- 系统学习下python网络爬虫 笔记一
- arm 网络调试内核及文件系统(tftp下载内核,nfs挂载根文件系统)/x86下nfs挂载根文件系统
- iOS9 适配网络请求,适配分享失败,适配无法正常跳转到客户端
- HTTP 的重定向301,302,303,307
- Spark数据挖掘-基于 K 均值聚类的网络流量异常检测(1): 数据探索、模型初探
- Wireshark网络分析就这么简单 -- 目录
- 安卓网络编程1--ANR异常和Handler机制
- Java网络编程
- Linux学习之命令(网络、关机)
- Node.js实践HTTP安全认证之二~~摘要认证(理论)
- springmvc No mapping found for HTTP request with URI in Dispatc