如何为GeckoView设置cookie?

时间:2019-05-01 20:35:30

标签: android gecko

我有一个Android WebView代码,在其中设置了cookie值。但是,相同的操作在Mozilla GeckoView中不起作用。

使用WebView的示例:

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    webView = view.findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);

    final CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(getContext());
    final CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);

    cookieManager.setCookie(url, "sessionid=123; path=/");
    cookieSyncManager.sync();

    webView.loadUrl("https://example.com");
}

带有GeckoView的示例:

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    webView = view.findViewById(R.id.webView); // GeckoView
    GeckoSession geckoSession = new GeckoSession();
    geckoSession.getSettings().setAllowJavascript(true);

    GeckoRuntimeSettings.Builder builder = new GeckoRuntimeSettings.Builder()
            .javaScriptEnabled(true);

    GeckoRuntime geckoRuntime = GeckoRuntime.create(Objects.requireNonNull(getContext()), builder.build());
    webView.setSession(geckoSession, geckoRuntime);

    final CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(getContext());
    final CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);

    cookieManager.setCookie(url, "sessionid=123; path=/");
    cookieSyncManager.sync();

    geckoSession.loadUri("https://example.com");
}

我没有收到任何错误,但cookie值不会影响加载的页面。

1 个答案:

答案 0 :(得分:0)

GeckoView目前不支持设置/获取cookie(2019年10月)。您可以对该错误的实现进行关注/投票:https://bugzilla.mozilla.org/show_bug.cgi?id=1035417