如果cookie过期,如何在webview中重定向到不同的URL?

时间:2017-01-30 04:45:09

标签: android cookies webview

每当登录cookie过期时,我必须检查加载URL是否以某个字符串结尾,然后我必须用另一个URL重定向它。我怎么能这样做?

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith("admin/login")) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("cookies", null).apply();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                    //  Log.d(C.TAG, "Using clearCookies code for API >=" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
                    CookieManager.getInstance().removeAllCookies(null);
                    CookieManager.getInstance().flush();
                } else {
                    //  Log.d(C.TAG, "Using clearCookies code for API <" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
                    CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(MainActivity.this);
                    cookieSyncMngr.startSync();
                    CookieManager cookieManager = CookieManager.getInstance();
                    cookieManager.removeAllCookie();
                    cookieManager.removeSessionCookie();
                    cookieSyncMngr.stopSync();
                    cookieSyncMngr.sync();

                }
                view.clearHistory();
                view.clearCache(true);
                view.loadUrl(URL_BASIC);
            }  return true;
        }

在这种情况下,通过 admin / login 网址重复拨打电话。它不会加载我提供的URL。

0 个答案:

没有答案