Android使用AsynchronousHttpClient

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代码   收藏代码
  1. PersistentCookieStore myCookieStore = new PersistentCookieStore(this);  
  2. 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代码   收藏代码
  1. BasicClientCookie newCookie = new BasicClientCookie("cookiesare""awesome");  
  2. newCookie.setVersion(1);  
  3. newCookie.setDomain("mydomain.com");  
  4. newCookie.setPath("/");  
  5. myCookieStore.addCookie(newCookie);  

See the PersistentCookieStore Javadoc for more information. 


开篇边有一句话描述“utomatically saves cookies to SharedPreferences storage ” 
自动保存cookie到SharedPreferences 中 

需要注意的一点是下面设置cookie的代码 
Java代码   收藏代码
  1. PersistentCookieStore myCookieStore = new PersistentCookieStore(this);  
  2. myClient.setCookieStore(myCookieStore);  

必须在client发起请求之前执行,然后再去执行client的get或者post请求。做好设置之后,它在请求之后才会把返回http的head中获取cookie保存。 
否则是无法保存的。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值