默认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. 357. Count Numbers with Unique Digits

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  2. UVa 814邮件传输代理的交互

    好吧,终于知道原来string有这么多用法,map可以通过vector来映射多个数 #include <iostream> #include <string> #include ...

  3. php使用redis存储

    一.Redis扩展模块 # wget https://codeload.github.com/phpredis/phpredis/zip/develop -O phpredis.zip # unzip ...

  4. hihoCoder#1014 Trie树 (前缀树)

    题目大意:给一本有n个单词的词典,有m次询问,每次询问的是该词典中有多少个单词有共同的某个前缀. 题目分析:在添加单词建立trie的时候,每经过一个节点就意味着该节点和它的各级祖先节点是某个单词的前缀 ...

  5. IOS请求H5页面、要求自定义agent判断是电脑、安卓还是iPhone登录

    //自定制的userAgent- (void)createMyAgent{        NSString *userAgent = [[[UIWebView alloc]init]stringByE ...

  6. 【NOIP2012】国王游戏

    这一次高精度完美地过辣好开心OvO,还get到了非常方便的高精度除小于10000的方法,这个是我自己脑出来的OvO 看来下午高精度傻逼得值qvq 原题: 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个 ...

  7. C++@命名空间(转)

    转自http://hi.baidu.com/rainysky_2006/blog/item/a490e01fc3de7964f724e4d1.html 本讲基本要求 * 掌握:命名空间的作用及定义:如 ...

  8. Linux驱动设计—— 中断与时钟@request_irq参数详解

    request_irq函数定义 /*include <linux/interrupt.h>*/ int request_irq(unsigned int irq, irq_handler_ ...

  9. 图中最短路径算法(Dijkstra算法)(转)

    1.Dijkstra 1)      适用条件&范围: a)   单源最短路径(从源点s到其它所有顶点v); b)   有向图&无向图(无向图可以看作(u,v),(v,u)同属于边集E ...

  10. 使用支持向量机训练mnist数据

    # encoding: utf-8 import numpy as np import matplotlib.pyplot as plt import cPickle import gzip clas ...