Android的会话管理Android

由网友(帅气的3个字的分享)分享简介:是否有Android的会话管理特定库?我需要管理我会在一个正常的Andr​​oid应用程序。不在的WebView 。我可以从我的文章的方法设置会话。但是,当我发送另一个请求会话丢失。有人可以帮助我这件事?Is there a specific library for Android session managemen...

是否有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.

阅读全文

相关推荐

最新文章