Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a cookie in an Android webview forever?

With my code below, I have been able to save a cookie, but as soon as I close the application the cookie disappears.

How is this caused and how can I solve it?

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

   }
}
like image 806
Nick Chubb Avatar asked Dec 05 '11 18:12

Nick Chubb


People also ask

Does Android WebView store cookies?

WebViews automatically persist cookies into a android. webkit. CookieManager. Need to store cookies from a WebView into a java.

Can we save login data in Android WebView?

Saving passwords in WebView will not be supported in future versions. Retrieves HTTP authentication credentials for a given host and realm from the WebViewDatabase instance.

Do cookies work in WebView?

By default this is set to true and the WebView accepts cookies.

How do I set cookies in WebView?

It's quite simple really. String cookieString = "cookie_name=cookie_value; path=/"; CookieManager. getInstance(). setCookie(baseUrl, cookieString);


2 Answers

You have to tell the CookieSyncManager to sync after it has loaded the page in question. In your sample code, the onCreate method executes completely before the WebView tries to load the page, so the sync process (which happens asynchronously) will probably complete before the page is loaded.

Instead, tell the CookieSyncManager to sync onPageFinished in the WebViewClient. That should get you what you want.

The CookieSyncManager Documentation is a good read for how to do this properly.

Here is how you could set up your WebViewClient implementation to do this for you:

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();     } ); 

You would not need to tell the CookieSyncManager to sync elsewhere with this in place. I haven't tested this, so let me know if it works.

like image 66
lyricsboy Avatar answered Sep 16 '22 22:09

lyricsboy


.sync() is to force a imediate sync ,and must be called after page load cause it sync RAM with cache , so cookie must be in ram before calling it .

System automatically sync it every 5 mintues if you use this scheme

onCreate: 
CookieSyncManager.createInstance(context)

onResume:
CookieSyncManager.getInstance().startSync()

onPause:
CookieSyncManager.getInstance().stopSync()

I think you did not waited 5 mintues so system save cookie.

like image 43
CGeorgian Avatar answered Sep 17 '22 22:09

CGeorgian