android_储存_cookie
来源:互联网 发布:打车软件分析报告 编辑:程序博客网 时间:2024/06/05 10:47
httpclient相当强大,读写cookie非常容易:
//读cookie
}
}
//设置cookie
myApp appCookie = ((myApp)getApplication());
myApp appCookie = ((myApp)getApplicationContext());
cookies = appCookie.getCookie();
从一个httpclient取得statuscode(状态码)的方法:
HttpResponse response = client.execute(mypost);
int statuscode = response.getStatusLine().getStatusCode();
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
cookie = cookies.get(i);
添加到POST请求
HttpPost httpPost = new HttpPost(访问地址);
httpPost.setHeader("Cookie", "JSESSIONID=" + 我们在静态变量里存放的SessionId);
HttpResponse httpResponse = httpclient.execute(httpPost);
添加到GET请求
HttpGet request = new HttpGet(url+"?"+Params);
request.setHeader("Cookie",Sessionid);
//读cookie
CookieStore cookies=((AbstractHttpClient)client).getCookieStore();
//写cookie
另外的一个问题是,为了在各个activity之间使用一个共通的cookie,需要一个全局变量来解决问题。
对于java来说,可以使用静态类,但是对于android来说,更符合android结构的作法是使用拥有这些activity的application类:
public class myApp extends Application
{
private CookieStore cookies;public CookieStore getCookie()
{
return cookies;}
public void setCookie(CookieStore cks)
{
cookies = cks;}
}
另外,要在AndroidManifest.xml文件中将上面声明的这个类绑定到包含所有activity的application上
方法: 在application标签加 android:name=".myApp"
//设置cookie
myApp appCookie = ((myApp)getApplication());
appCookie.setCookie(cookies);
myApp appCookie = ((myApp)getApplicationContext());
cookies = appCookie.getCookie();
从一个httpclient取得statuscode(状态码)的方法:
HttpResponse response = client.execute(mypost);
int statuscode = response.getStatusLine().getStatusCode();
第二个怎么获得服务器的cookie呢,知道这次的session id,通过Httpclient 中的getcookiestore
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty())
{
Log.i(TAG, "-------Cookie NONE---------");}
else
{
for (int i = 0; i < cookies.size(); i )
{
//保存cookiecookie = cookies.get(i);
Log.d(TAG, cookies.get(i).getName() "=" cookies.get(i).getValue() );
}
}添加到POST请求
HttpPost httpPost = new HttpPost(访问地址);
httpPost.setHeader("Cookie", "JSESSIONID=" + 我们在静态变量里存放的SessionId);
HttpResponse httpResponse = httpclient.execute(httpPost);
添加到GET请求
HttpGet request = new HttpGet(url+"?"+Params);
request.setHeader("Cookie",Sessionid);
0 0
- android_储存_cookie
- android_巧用SharedPrefrences储存ArrayList等Collection的实现类
- $_COOKIE 默认保存时间
- PHP _cookie&session
- 15_cookie(一)
- 16_cookie(二)
- 17_cookie(三)
- JSP案例_Cookie传值
- 黑马程序员_Cookie学习总结
- 【JavaScript】document对象_Cookie属性
- 18_cookie(四_补充)
- http协议_Cookie 和 Session
- Java_乔晓松_Cookie技术--会话管理
- javaweb10——会话_Cookie/HttpSession
- php $_GET $_POST $_COOKIE取值过程
- get_magic_quotes_gpc() 对$_GET $_COOKIE $_POST转义处理
- PHP 里面$_REQUEST 包含 $_GET,$_POST,$_COOKIE
- 内部储存,外部储存。
- 1-Nancy文档教程——简介
- Codeforces #258 (Div. 2) B. Sort the Array
- 对于效率的一点感慨
- 列:取第几列的内容20140724 ①文本处理
- linux zip压缩和解压的各种操控
- android_储存_cookie
- Codeforces #258 (Div. 2) A. Game With Sticks
- Linux的僵尸进程解决攻略
- 对教育的一点看法
- gfwgwsgwsgw
- fgvwgwgwggwgw
- 阿萨德发福
- fgwggwgwgwgw
- Codeforces #258 (Div. 2) C. Predict Outcome of the Game