2011-12-05 88 views
15

使用我的代码,我可以保存一个cookie,但是一旦关闭应用程序,cookie就会消失。如何将cookie永久保存在Android Web浏览器中?

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

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://"); 

    } 
} 
+0

CookieSyncManager现在已被弃用,使用CookieManager。 http:// stackoverflow。com/questions/30502411/cookiesyncmanager-is-now-deprecated-what-c​​an-use-instead –

+0

@Rahul Sahni我尝试过,但如果关闭应用程序并重新打开,我必须重新登录,所有cookies丢失!你能告诉我可能是什么问题吗? – user1788736

回答

35

你必须告诉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到位与此同步的其他地方。我没有测试过,所以让我知道它是否有效。

+0

我是全新的......你能给我一些实际的代码吗?这将有助于一吨! – Joe

+1

你走了。给它一个旋转。 – lyricsboy

+2

你摇滚!这很好,纯粹的天才!多谢,伙计! – Joe

4

.sync()强制执行同步操作,并且必须在页面加载后调用它使RAM与缓存同步,因此在调用它之前,Cookie必须在ram中。如果你使用这个方案

onCreate: 
CookieSyncManager.createInstance(context) 

onResume: 
CookieSyncManager.getInstance().startSync() 

onPause: 
CookieSyncManager.getInstance().stopSync() 

我想你没有等待5分钟路程,从而节省系统饼干

系统自动同步它每隔5分钟路程。

+0

我该怎么办如果我想立即同步cookie,而无需在android 6上等5分钟? – user1788736

0

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

请注意,我还没有尝试过,所以如果它的作品,请让我知道。

根据Android文档

void flush()

确保目前通过的getCookie API访问的所有Cookie被写入到永久存储。这个调用将阻止调用者,直到它完成并且可以执行I/O。

此外,在CookieSyncManager documnentation它写的是:

这个类是在API级别21 不推荐使用的WebView现在会自动同步必要的饼干。您不再需要创建或使用CookieSyncManager。要手动强制同步,您可以使用CookieManager方法flush(),它是sync()的同步替换。 CookieSyncManger link

+0

@ user1788736希望这可以帮助你 –