22

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

   }
}
4

4 回答 4

44

您必须告诉 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();
    }
);

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

于 2011-12-05T18:49:29.483 回答
5

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

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

onCreate: 
CookieSyncManager.createInstance(context)

onResume:
CookieSyncManager.getInstance().startSync()

onPause:
CookieSyncManager.getInstance().stopSync()

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

于 2014-12-22T03:21:48.453 回答
5

对于那些在关闭应用程序后仍然存在类问题的人,CookieManager他们应该尝试flush().CookieManager

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

根据android文档

无效冲洗()

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

同样在CookieSyncManager文档中写道:

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

于 2017-12-20T19:20:43.877 回答
0

完美适用于 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();
            }

        }
        
        );

于 2021-09-20T23:34:30.327 回答