如何永久保存Android webview中的cookie?

Joe*_*Joe 19 java cookies android webview cookiestore

使用下面的代码,我已经能够保存cookie,但是一旦我关闭应用程序,cookie就会消失.

这是怎么造成的,我该如何解决?

package com.jkjljkj

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class Activity extends Activity {

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        CookieSyncManager.createInstance(getBaseContext());

        // Let's display the progress in the activity title bar, like the
        // browser app does.


        getWindow().requestFeature(Window.FEATURE_PROGRESS);

        WebView webview = new WebView(this);
        setContentView(webview);


        webview.getSettings().setJavaScriptEnabled(true);

        final Activity activity = this;
        webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
             // Activities and WebViews measure progress with different scales.
             // The progress meter will automatically disappear when we reach 100%
             activity.setProgress(progress * 1000);
        }
      });

      webview.setWebViewClient(new WebViewClient() {

         public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
              //Users will be notified in case there's an error (i.e. no internet connection)
              Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
         }
      });

      CookieSyncManager.getInstance().startSync();
      CookieSyncManager.getInstance().sync();

     //This will load the webpage that we want to see
      webview.loadUrl("http://");

   }
}
Run Code Online (Sandbox Code Playgroud)

lyr*_*boy 40

加载相关页面,您必须告诉CookieSyncManager要同步.在示例代码中,该onCreate方法在WebView尝试加载页面之前完全执行,因此同步过程(异步发生)可能会在加载页面之前完成.

相反,告诉CookieSyncManager在WebViewClient中同步onPageFinished.这应该会得到你想要的.

CookieSyncManager文档是如何做到这一点正确良好的阅读.

以下是如何设置WebViewClient实现来为您执行此操作:

webview.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        //Users will be notified in case there's an error (i.e. no internet connection)
        Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
    }

    public void onPageFinished(WebView view, String url) {
        CookieSyncManager.getInstance().sync();
    }
);
Run Code Online (Sandbox Code Playgroud)

您不需要告诉CookieSyncManager在其他位置同步.我没有测试过这个,所以请告诉我它是否有效.

  • 适用于所有来到这里并使用20以上API的人.不再需要CookieSyncManager,不推荐使用.(https://developer.android.com/reference/android/webkit/CookieSyncManager.html)现在可以自动同步cookie. (3认同)
  • 你摇滚!这完美地工作......纯粹的天才!多谢,伙计! (2认同)
  • 我喜欢程序员的相处:D干杯队友 (2认同)
  • @ Daan Pape。正如您提到的那样,cookies现在会自动同步,但是当我关闭该应用程序并重新打开时,我必须重新登录到webview上的页面!你知道是什么原因导致这个问题吗? (2认同)

小智 5

.sync() 是强制立即同步,必须在页面加载后调用,因为它会同步 RAM 与缓存,因此 cookie 在调用之前必须在 ram 中。

如果您使用此方案,系统会每 5 分钟自动同步一次

onCreate: 
CookieSyncManager.createInstance(context)

onResume:
CookieSyncManager.getInstance().startSync()

onPause:
CookieSyncManager.getInstance().stopSync()
Run Code Online (Sandbox Code Playgroud)

我想你没有等 5 分钟,所以系统保存了 cookie。


Rah*_*hni 5

对于那些谁与面临的问题CookieManager类,使饼干即使在关闭应用程序后仍然存在,他们应该尝试flush()的功能CookieManager

请注意,我还没有尝试过,所以如果它有效,也请告诉我。

根据android文档

无效冲洗()

确保当前可通过 getCookie API 访问的所有 cookie 都写入持久存储。这个调用将阻塞调用者直到它完成并且可以执行 I/O。

CookieSyncManager文档中还写道:

此类在 API 级别 21 中已弃用。WebView 现在根据需要自动同步 cookie。您不再需要创建或使用 CookieSyncManager。要手动强制同步,您可以使用 CookieManager 方法 flush(),它是 sync() 的同步替代品。CookieSyncManger 链接