2

Needed datas is not written to cookies immediately in WebView. But when I wait for 10-15 seconds everything is ok. To explain the situation, this example would be good as for me:

I open the app and login. After login, I close the app immediately. Then after I open the app again, it shows me to logout. But if I open the app after 1 minute, it shows again as logged in. For me cookies are written lately. But I cannot find solution. Please help me if you know.

I used CookieManager class but it doesn't help either.

CookieManager.getInstance().setAcceptCookie(true);
Johny
  • 595
  • 1
  • 6
  • 25
Vusala Hasanli
  • 400
  • 2
  • 6
  • 21

2 Answers2

5

I had similar issue and I added the below code and worked.

String myURL = "https://www.yourWebPage.com";

android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();

cookieManager.setAcceptCookie(true);
cookieManager.acceptCookie();
cookieManager.setAcceptFileSchemeCookies(true);
cookieManager.getInstance().setAcceptCookie(true);
cookieManager.getCookie(myURL); 

Hope it helps.

Johny
  • 595
  • 1
  • 6
  • 25
0

I had the similar problem, what I did is to get the cookies when login and set cookies for the load url you set on webView

        @Nullable
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            String resourceUrl = request.getUrl().toString();
            Log.e(MainActivity.TAG, "the request url :" + resourceUrl);
            CookieManager cookieManager1 = CookieManager.getInstance();
            // get the resourceUrl that has session
            if (resourceUrl.equals("the url has session")){
                String Cookies = cookieManager1.getCookie(resourceUrl);
                if (Cookies != null && Cookies.contains("sessionid")){
                    String[] cookiesList = Cookies.split(";");
                    cookieManager1.removeSessionCookies(null);
                    for (String c : cookiesList) {
                        cookieManager1.setCookie("the load url", c);
                    }
                }
            }
            return super.shouldInterceptRequest(view, request);

        }