Set Android Cookie

I just found out how to use webview in Android. Now I am trying to save a "dummy cookie" for my html page that opens in webview. I observe a somewhat strange behavior that sometimes has Cookies.dbin my / data / data / package / app _webview, and sometimes the cookie is not set and it does not exist.

What is the app_webview folder and what does it store exactly? Right now, I see a Cache folder in it, in which there are several files, and their names indicate that they are encrypted or, at least, should be bothered by the developer. So I ignored them. Besides this, sometimes I see Cookies.db and Web Data.db inside the app_webview folder.

My cookie setting code:

mWebView.loadUrl("http://10.0.2.2/cookieDemo.html");

Content of cookieDemo.html (if it matches):

<html>
   <head>  
      </script>  
   </head>
   <body>
      <form name="loginform">
         <input type="text" name="emppassNTL" />
         <button name="button" onclick="btnLogin.performClick(this.value);" style="height:30px;width:100px">Set Cookie</button>    
      </form>
   </body>
</html>

- cookie, html. , :

mWebView.addJavascriptInterface(new Object() {

        @JavascriptInterface
        public void performClick(String str1) {
            Toast.makeText(mWebView.getContext(), "Set Cookie button click", Toast.LENGTH_SHORT).show();
            // Need to add cookie to webview here.
            CookieManager manager = CookieManager.getInstance();

            manager.setAcceptCookie(true);

            manager.setCookie(URL, "testCookie");
            CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(mWebView.getContext());
            cookieSyncManager.sync();
        }
    }, "btnLogin");

sync CookieSyncManager , . Cookies.db , . , , , .

UPDATE: CookieSyncManager , cookie . , Cookies.db . , . , , sync() .

+4

Source: https://habr.com/ru/post/1622176/


All Articles