默认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
}
});
}
});
}
}

android下asynchttp库对于session的支持的更多相关文章

  1. Android下查看共享库依赖项

    Android下查看共享库依赖项 [时间:2017-02] [状态:Open] [关键词:android,共享库依赖项,so,ndk,objdump,readelf] 起因 近期在处理Android下 ...

  2. 解决 Android 中出现依赖多个版本支持库的问题

    在 app 的 build.gradle 中引入依赖时发现如下错误: All com.android.support libraries must use the exact same version ...

  3. Mac OS X10.10_xcode6.1_ios8.1环境下,编译lame静态库libmp3lame.a,支持arm64 armv7s x86_64 i386 armv7指令集

    近期升级了系统到Mac OS X 10.10 而且更新了XCode6.1和iOS 8.1 之前app用到的libmp3lame.a静态库.也要支持64位的模拟器(x86_64)和64位的真机(arm6 ...

  4. go 工具链目前[不支持编译 windows 下的动态链接库][1],不过[支持静态链接库][2]

    go 工具链目前[不支持编译 windows 下的动态链接库][1],不过[支持静态链接库][2].想要产生dll,可以这样 workaround ,参考 golang [issuse#11058][ ...

  5. [转]Android下怎么使用LDD查看依赖库

    Android下没有ldd可以使用,在进行ndk开发的时候,检查库的依赖项特别麻烦.有两个解决方案: 1.将linux的的ldd移植过去.因为android也是基于linux的,所以将ldd移植过去是 ...

  6. 【失败经验分享】android下使用支持opencl的cv::dft()

    1.使用了UMat,但是并未使用GPU计算 cv::dft()函数的定义是: void cv::dft( InputArray _src0, OutputArray _dst, int flags, ...

  7. Android 第三方开源库收集整理(转)

    原文地址:http://blog.csdn.net/caoyouxing/article/details/42418591 Android开源库 自己一直很喜欢Android开发,就如博客签名一样,  ...

  8. 45.Android 第三方开源库收集整理(转)

    原文地址:http://blog.csdn.net/caoyouxing/article/details/42418591 Android开源库 自己一直很喜欢Android开发,就如博客签名一样,  ...

  9. Android进阶笔记02:Android 网络请求库的比较及实战(二)

    一.Volley        既然在android2.2之后不建议使用HttpClient,那么有没有一个库是android2.2及以下版本使用HttpClient,而android2.3及以上版本 ...

随机推荐

  1. Jquery.KinSlideshow图片轮播插件

    KinSlideshow无缝滑动幻灯片jquery特效代码Jquery幻灯片特效jquery.KinSlideshow-1.1.js 兼容IE6/IE7/IE8/IE9,FireFox,Chrome* ...

  2. Gridview中DataKeyNames 设置多个主键 取值

    1.设置DataKeyNames a.F4  在属性面板中设置   多个值以逗号隔开  例如id,mane,sex b.通过后台代码 this.gridview.DataSource = Bind() ...

  3. Web服务器IPtables配置

    #允许SSH流量(重要) iptables -A INPUT -p tcp --dport 22 -j ACCEPT iptables -A INPUT -p tcp --dport 222 -j A ...

  4. 内存泄漏,当您使用的 GetDC 方法和 ReleaseDC 方法 CWnd 类版本

    重现行为的步骤 是从 CWnd 派生的类的一个方法中插入下面的代码在您的应用程序中: CDC *pDC; RECT rect; GetClientRect (&rect); for (int ...

  5. c++中的dictionary对象:map的使用备忘

    #include <map> #include <iostream> using namespace std; void main(){ map <string, int ...

  6. rsyslog

    http://www.rsyslog.com/ http://www.rsyslog.com/doc/v5-stable/troubleshooting/troubleshoot.html RSYSL ...

  7. linux包之sysstat之sar命令

    要启动SAR,必须通过cron工具以周期性的间隔启动.安装sysstat包后,默认创建一个/etc/cron.d/sysstat文件,其默认内容为:# run system activity acco ...

  8. CentOS6.4系统启动失败故障排查

    转:http://www.centoscn.com/CentosBug/osbug/2014/1028/4011.html 操作系统启动失败如下图报错: 故障现象: 从图中可以看到,操作系统启动的过程 ...

  9. 1 TKinter小窗口及标题

    说明 :本博客户关于tkinter的知识参考:2014 年辛星 Python 界面编程教程第二版 创建一个GUI程序的步骤: 创建一个GUI程序 1.导入Tkinter模块 2.创建控件 3.指定这个 ...

  10. ManagementFactory cannot be resolved

    ManagementFactory cannot be resolved 问题描述: (1)ManagementFactory cannot be resolved or (2)Type The ty ...