在android中获取cookie形式cookieManager.getCookieStore()的值

时间:2016-05-25 06:54:47

标签: android eclipse cookies httpcookie

SetCookie函数应该设置cookie并且GetCookie应该读取cookie的值.SetCookie工作正常但getcookie遇到异常beacuse cookieManager.getCookieStore().get(new URI("https://archive.irib.ir/"))返回空字符串并且它的大小为0.我怎么能读取我的cookie的值????

这是我的SetCookie和GetCookie

    public void SetCookie(String Value) {
    try
    {
        CookieManager cookieManager = new CookieManager();
        CookieHandler.setDefault(cookieManager);
        HttpCookie cookie = new HttpCookie("lang", Value);

        cookie.setDomain("archive.irib.ir");
        cookie.setPath("/");
        cookie.setVersion(0);
        cookie.setValue(Value);
        cookieManager.getCookieStore().add(new URI("https://archive.irib.ir/"), cookie);

    }
    catch(Exception ex)
    {


    }
}
public String GetCookie() {
    try
    {
        CookieManager cookieManager = new CookieManager();
        List<HttpCookie> Cookies =  cookieManager.getCookieStore().get(new URI("https://archive.irib.ir/"));
        //Integer s = Cookies.size();
        return Cookies.get(0).getValue().toString();
    }
    catch(Exception ex)
    {
        return ex.getMessage();
    }

}

1 个答案:

答案 0 :(得分:0)

猜猜你在每个函数中使用不同的cookieManager实例,尝试创建一个静态cookieManager实例或者像成员类一样定义它。