I have an Android application which has a login screen. Upon the click of login button I am launching a webview with cookies passed to it. So, I am able to see the contents of the webpage in the webview.

But when I click on the links of the webpage loaded, then the request is faling and from there webpage is not loading. How to pass cookies in such a way that after the login , I can traverse through any number of webpages relentlessly.

有帮助吗?

解决方案

Try This..

    WebView webview = new WebView(this);
    webview.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }
    });

    CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(webview.getContext());
    cookieSyncManager.startSync();
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.removeSessionCookie();
    cookieManager.setCookie(URL, COOKIE);
    cookieSyncManager.sync();

    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl(URL);
    setContentView(webview);

其他提示

You should not leave your WebView. Intercept the link and load the URL in your own WebView

  WebView webview = new WebView(this);      
      webview.setWebViewClient(new WebViewClient() {  
          @Override  
          public boolean shouldOverrideUrlLoading(WebView view, String url)  
          {  
               view.loadUrl(url);  
               return true;  
          }  
        });                 
      setContentView(webview);      
      webview.loadUrl(yourUrl);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top