android下asynchttp库对于session的支持
2024-08-20 16:07:51
默认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
}
});
}
});
}
}
最新文章
- VBA续嘘嘘
- NOIp Graph 1002 瞎眼记
- Ruby on Rails框架开发学习
- android GC内存回收小析
- unity工具IGamesTools之批量生成帧动画
- Linux 内存布局
- .NET定时发送邮件
- SharePoint 2013 代码实现自定义的站点模版创建Site Collection
- Linux红黑树(二)——访问节点
- C/S应用升级更新完整解决方案
- 关于InnoDB的读写锁类型以及加锁方式
- 树莓派3B+ 安装系统
- Python(day1)
- JavaScript原生秒表、计时器
- 第三节,TensorFlow 使用CNN实现手写数字识别(卷积函数tf.nn.convd介绍)
- Docker(九)-Docker创建Selenium容器
- Android sdk安装目录中没有platform-tools目录问题详解
- iOS 建立项目过滤机制 —— 给工程添加忽略文件.gitignore
- 错误:Unsupported major.minor version 51.0(jdk版本错误)
- redis集群搭建+lua脚本的使用
热门文章
- 搭建 hexo,在执行 hexo deploy 后,出现 error deployer not found:github 的错误
- hdu 5206 Four Inages Strategy
- SQL注入测试平台 SQLol -1. 简介与安装
- abbyy cup a
- 制作自己的ros机器人(navigaion)前提--22
- makefile--模式规则(七)
- 最大连续子序列和问题(Maximum Consecutive Subsequence Sum)
- linux缓冲的概念fopen /open,read/write和fread/fwrite区别
- Integer相加产生的类型转换问题
- JAVA导入包