Android版的HTTP cookie [英] Android HTTP cookie

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

问题描述

我想我的Andr​​oid应用和我的Drupal网站之间保持的loggedIn用户会话。在我的研究,把它归结为发送的cookie(S)回Drupal的,但我在努力寻找一种资源来帮助指导我。

I am trying to maintain a loggedin user session between my android app and my Drupal website. In my research, it comes down to sending cookie(s) back to Drupal but I am struggling to find a resource to help guide me.

谁能推荐一个很好的教程,我可以用它来完成供电的Andr​​oid和Drupal之间持续连接一个cookie,好吗?

Can anyone recommend a good tutorial that I can use to accomplish a cookie powered persistent connection between Android and Drupal, please?

推荐答案

以防万一别人得到了同样的问题,我有类似的问题,我能够通过以下code解决这个问题:

Just in case anyone else got the same issue, I had similar problem and I was able to solve it by the following code:

1定义CookieManager并在CookieStore的类

1- Define CookieManager and CookieStore in your class

CookieManager cookieManager;
CookieStore cookieStore;

2 - 添加默认的cookie处理程序,例如在类的构造函数或onCreate方法

2- Add default cookie handler, e.g. in the class constructor or in OnCreate method

cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);

3,当你做HTTP请求使用cookie存储

3- Use the cookie storage when you do HTTP request

public byte[] openURI(String uri) {

    try {
        URI uriObj = new URI(uri);
        DefaultHttpClient client = new DefaultHttpClient();

        // Use the cookieStor with the request
        if (cookieStore == null) {
            cookieStore = client.getCookieStore();
        } else {
            client.setCookieStore(cookieStore);
        }

        HttpGet getRequest = new HttpGet(uriObj);
        HttpResponse response = client.execute(getRequest);

        // Read the response data
                    InputStream instream = response.getEntity().getContent();
        int contentLength = (int) response.getEntity().getContentLength();
        byte[] data = new byte[contentLength];
        instream.read(data);
        response.getEntity().consumeContent();
        return data ;

    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;        
}

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

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