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):
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保存。
否则是无法保存的。

更多相关文章

  1. 【知识点】android代码中设置margin
  2. Android蓝牙通信代码
  3. Android代码规范
  4. android典型代码系列(二十二)------按键使上面的EditText抖动
  5. 用代码创建Android视图
  6. Android中对Group的各种操作示例代码
  7. Android 获取时间实例代码
  8. android 源代码 获得 Open Soure Project

随机推荐

  1. Android 3.1 r1 中文API文档 (120) ——
  2. Android客户端与服务器用Socket进行通信
  3. 【专题】Android 启动流程相关
  4. Android 安卓让LinearLayout放置于底部的
  5. Android UI 优化-使用theme 预加载
  6. android textview 实现跑马灯效果
  7. SQLite语法与Android数据库操作
  8. setEmptyView
  9. Android UI(1)Getting Started - First A
  10. 【Android】性能优化的一些方法