Example usage for android.webkit CookieManager setCookie

List of usage examples for android.webkit CookieManager setCookie

Introduction

In this page you can find the example usage for android.webkit CookieManager setCookie.

Prototype

public abstract void setCookie(String url, String value);

Source Link

Document

Sets a cookie for the given URL.

Usage

From source file:com.codename1.impl.android.AndroidImplementation.java

public void addCookie(Cookie c, boolean addToWebViewCookieManager, boolean sync) {
    if (addToWebViewCookieManager) {
        CookieManager mgr;
        CookieSyncManager syncer;/*from w ww .ja va2s. co m*/
        try {
            syncer = CookieSyncManager.getInstance();
            mgr = getCookieManager();
        } catch (IllegalStateException ex) {
            syncer = CookieSyncManager.createInstance(this.getContext());
            mgr = getCookieManager();
        }
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z");
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        String cookieString = c.getName() + "=" + c.getValue() + "; Domain=" + c.getDomain() + "; Path="
                + c.getPath() + "; " + (c.isSecure() ? "Secure;" : "") + (c.isHttpOnly() ? "httpOnly;" : "")
                + (c.getExpires() != 0 ? ("Expires=" + format.format(new Date(c.getExpires())) + ";") : "");
        mgr.setCookie("http" + (c.isSecure() ? "s" : "") + "://" + c.getDomain() + c.getPath(), cookieString);
        if (sync) {
            syncer.sync();
        }
    }
    super.addCookie(c);

}

From source file:com.codename1.impl.android.AndroidImplementation.java

public void addCookie(Cookie[] cs, boolean addToWebViewCookieManager, boolean sync) {
    if (addToWebViewCookieManager) {
        CookieManager mgr;
        CookieSyncManager syncer;/*www .  j  a  va2s . c o  m*/
        try {
            syncer = CookieSyncManager.getInstance();
            mgr = getCookieManager();
        } catch (IllegalStateException ex) {
            syncer = CookieSyncManager.createInstance(this.getContext());
            mgr = getCookieManager();
        }
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z");
        format.setTimeZone(TimeZone.getTimeZone("GMT"));

        for (Cookie c : cs) {
            String cookieString = c.getName() + "=" + c.getValue() + "; Domain=" + c.getDomain() + "; Path="
                    + c.getPath() + "; " + (c.isSecure() ? "Secure;" : "")
                    + (c.getExpires() != 0 ? (" Expires=" + format.format(new Date(c.getExpires())) + ";") : "")
                    + (c.isHttpOnly() ? "httpOnly;" : "");
            mgr.setCookie("http" + (c.isSecure() ? "s" : "") + "://" + c.getDomain() + c.getPath(),
                    cookieString);

        }

        if (sync) {
            syncer.sync();
        }
    }
    super.addCookie(cs);

}