Android的会话管理 [英] Android session management

查看:169
本文介绍了Android的会话管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有Android的会话管理特定库?我需要管理我会在一个正常的Andr​​oid应用程序。不在的WebView 。我可以从我的文章的方法设置会话。但是,当我发送另一个请求会话丢失。有人可以帮助我这件事?

Is there a specific library for Android session management? I need to manage my sessions in a normal Android app. not in WebView. I can set the session from my post method. But when I send another request that session is lost. Can someone help me with this matter?

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("My url");

HttpResponse response = httpClient.execute(httppost);
List<Cookie> cookies = httpClient.getCookieStore().getCookies();

if (cookies.isEmpty()) {
    System.out.println("None");
} else {
    for (int i = 0; i < cookies.size(); i++) {
        System.out.println("- " + cookies.get(i).toString());
    }
}

当我试图访问同一主机的会话丢失:

When I try to access the same host that session is lost:

HttpGet httpGet = new HttpGet("my url 2");
HttpResponse response = httpClient.execute(httpGet);

我得到的登录页面响应主体。

I get the login page response body.

推荐答案

这无关与Android。它拥有一切做与Apache HttpClient的,你正在使用HTTP访问的库。

This has nothing to do with Android. It has everything to do with Apache HttpClient, the library you are using for HTTP access.

会话cookie保存在您的 DefaultHttpClient 对象。而不是创建一个新的 DefaultHttpClient 为每个请求的,守住它,并重新使用它,你的会话cookie将保持不变。

Session cookies are stored in your DefaultHttpClient object. Instead of creating a new DefaultHttpClient for every request, hold onto it and reuse it, and your session cookies will be maintained.

您可以阅读有关Apache的HttpClient 这里和HttpClient的了解cookie管理的这里

You can read about Apache HttpClient here and read about cookie management in HttpClient here.

这篇关于Android的会话管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆