【问题标题】:How to save a cookie in an Android webview forever?如何将 cookie 永久保存在 Android webview 中?
【发布时间】:2012-01-13 11:30:03
【问题描述】:

使用下面的代码,我已经能够保存一个 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://");

   }
}

【问题讨论】:

  • CookieSyncManager 现已弃用,请使用 CookieManager。 stackoverflow.com/questions/30502411/…
  • @Rahul Sahni 我试过了,但如果关闭应用程序并重新打开,我必须重新登录,所有 cookie 都会丢失!你能告诉我可能是什么问题吗?

标签: java android cookies webview cookiestore


【解决方案1】:

完美适用于 API 20+

        myWebView.setWebViewClient(new WebViewClient(){
            public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl){
                Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();

            }

            public void onPageFinished(WebView webView, String url){
                CookieManager.getInstance().flush();
            }

        }
        
        );

【讨论】:

    【解决方案2】:

    对于那些面临CookieManager类问题,即使在关闭应用程序后仍然存在cookie,他们应该尝试CookieManagerflush()功能。

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

    根据安卓文档

    void flush()

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

    同样在CookieSyncManager 文档中写道:

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

    【讨论】:

      【解决方案3】:

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

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

      onCreate: 
      CookieSyncManager.createInstance(context)
      
      onResume:
      CookieSyncManager.getInstance().startSync()
      
      onPause:
      CookieSyncManager.getInstance().stopSync()
      

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

      【讨论】:

      • 如果我想立即同步 cookie 而无需在 android 6 上等待 5 分钟,我该怎么办?
      【解决方案4】:

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

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

      CookieSyncManager Documentation 是一本关于如何正确执行此操作的好书。

      您可以通过以下方式设置 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();
          }
      );
      

      您不需要告诉 CookieSyncManager 与其他地方同步。我还没有测试过,所以请告诉我它是否有效。

      【讨论】:

      • 我是全新的...你能给我一些实际的代码吗?这将有很大帮助!
      • 你去。试一试。
      • 我喜欢程序员的相处方式:D Cheers mates
      • 适用于所有来到这里并使用 API 晚于 20 的人。不再需要 CookieSyncManager 并且已弃用。 (developer.android.com/reference/android/webkit/…) cookie 现在会自动同步。
      • @Daan Pape。正如您所提到的,cookie 现在会自动同步,但是当我关闭应用程序并重新打开时,我必须再次在 webview 上重新登录页面!你知道是什么导致了这个问题吗?
      猜你喜欢
      • 1970-01-01
      • 2015-08-24
      • 2022-01-22
      • 2012-11-25
      • 1970-01-01
      • 2019-02-28
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多