Android使用Asynchronous Http Client完成登录保存cookie的问题
来源:互联网 发布:获取端口号 编辑:程序博客网 时间:2024/06/09 03:13
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):
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:
See the PersistentCookieStore Javadoc for more information.
开篇边有一句话描述“utomatically saves cookies to SharedPreferences storage ”
自动保存cookie到SharedPreferences 中
需要注意的一点是下面设置cookie的代码
必须在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):
- 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:
- 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的代码
- PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
- myClient.setCookieStore(myCookieStore);
必须在client发起请求之前执行,然后再去执行client的get或者post请求。做好设置之后,它在请求之后才会把返回http的head中获取cookie保存。
否则是无法保存的。
0 0
- Android使用Asynchronous Http Client完成登录保存cookie的问题
- Instagram 和 Pinterest使用的Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- ios学习之UISwipeGestureRecognizer手势识别
- C语言练习-指针(数组,字符串)
- 博客搬家通知!!!
- C 语言程序设计实践 4.3 龟兔赛跑
- iOS开发 delegate,SEL和Block
- Android使用Asynchronous Http Client完成登录保存cookie的问题
- [leetCode] Regular Expression Matching
- 【LeetCode】Merge Two Sorted Lists
- Visual Studio 2013 C/C++单元测试
- 学生管理系统源代码
- C 语言程序设计实践 4.4 车牌号
- ios开发——用UITableView展示多行多列图片
- 分布式爬虫系统
- C 语言程序设计实践 4.5 四边形