安卓会话管理 [英] Android session management

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

问题描述

是否有特定的 Android 会话管理库?我需要在普通的 Android 应用程序中管理我的会话.不在 WebView 中.我可以从我的 post 方法设置会话.但是当我发送另一个请求时,该会话丢失了.有人可以帮我解决这个问题吗?

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 的信息并阅读有关 cookie 管理的信息在 HttpClient 这里.

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

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

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