默认asynchttp库不支持session,需要用户配置下cookie来处理,直接贴支持session的代码

package example.com.sessiontest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore; import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.client.CookieStore;
import cz.msebera.android.httpclient.client.protocol.ClientContext;
import cz.msebera.android.httpclient.cookie.Cookie;
import cz.msebera.android.httpclient.protocol.HttpContext; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button testButton = (Button)findViewById(R.id.button);
testButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { AsyncHttpClient client = new AsyncHttpClient(); HttpContext httpContext = client.getHttpContext();
CookieStore cookies = (CookieStore) httpContext.getAttribute(ClientContext.COOKIE_STORE);//获取AsyncHttpClient中的CookieStore
if(cookies!=null){
for(Cookie c:cookies.getCookies()){
System.out.println("main before ~~" + c.getName() + c.getValue());
}
}else{
System.out.println("main before~~" + "cookies is null");
}
PersistentCookieStore myCookieStore = new PersistentCookieStore(MainActivity.this);
client.setCookieStore(myCookieStore);
httpContext = client.getHttpContext();
cookies = (CookieStore) httpContext.getAttribute(ClientContext.COOKIE_STORE);
if(cookies!=null){
for(Cookie c:cookies.getCookies()){
System.out.println("main after ~~" + c.getName() + c.getValue());
}
}else{
System.out.println("main after~~" + "cookies is null");
} client.get("http://192.168.0.150:8080/test", new AsyncHttpResponseHandler() { @Override
public void onStart() {
// called before request is started
} @Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
String responseString = new String(response);
System.out.println(responseString);
} @Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
} @Override
public void onRetry(int retryNo) {
// called when request is retried
}
});
}
});
}
}

最新文章

  1. VBA续嘘嘘
  2. NOIp Graph 1002 瞎眼记
  3. Ruby on Rails框架开发学习
  4. android GC内存回收小析
  5. unity工具IGamesTools之批量生成帧动画
  6. Linux 内存布局
  7. .NET定时发送邮件
  8. SharePoint 2013 代码实现自定义的站点模版创建Site Collection
  9. Linux红黑树(二)——访问节点
  10. C/S应用升级更新完整解决方案
  11. 关于InnoDB的读写锁类型以及加锁方式
  12. 树莓派3B+ 安装系统
  13. Python(day1)
  14. JavaScript原生秒表、计时器
  15. 第三节,TensorFlow 使用CNN实现手写数字识别(卷积函数tf.nn.convd介绍)
  16. Docker(九)-Docker创建Selenium容器
  17. Android sdk安装目录中没有platform-tools目录问题详解
  18. iOS 建立项目过滤机制 —— 给工程添加忽略文件.gitignore
  19. 错误:Unsupported major.minor version 51.0(jdk版本错误)
  20. redis集群搭建+lua脚本的使用

热门文章

  1. 搭建 hexo,在执行 hexo deploy 后,出现 error deployer not found:github 的错误
  2. hdu 5206 Four Inages Strategy
  3. SQL注入测试平台 SQLol -1. 简介与安装
  4. abbyy cup a
  5. 制作自己的ros机器人(navigaion)前提--22
  6. makefile--模式规则(七)
  7. 最大连续子序列和问题(Maximum Consecutive Subsequence Sum)
  8. linux缓冲的概念fopen /open,read/write和fread/fwrite区别
  9. Integer相加产生的类型转换问题
  10. JAVA导入包