Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, catch webview redirection url

My web view loads a url that - after completing loading - gets changed to another url. how can I catch the new url. getURL() always returns the 1st url not the second. I can see the new URL if i use a browser but I can't get if from the webview.

like image 775
a fair player Avatar asked Nov 07 '13 09:11

a fair player


2 Answers

You could use a webClient and implement shouldOverrideUrlLoading to intercept all the urls before the WebView loads them.

    mWebView.setWebViewClient(new WebViewClient() {


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
           // Here put your code
              Log.d("My Webview", url);

           // return true; //Indicates WebView to NOT load the url;
              return false; //Allow WebView to load url
        }
    });
like image 103
jDur Avatar answered Nov 06 '22 06:11

jDur


Use

getOriginalUrl () 

It returns the URL that was originally requested for the current page

getUrl () is not always the same as the URL passed to WebViewClient.onPageStarted because although the load for that URL has begun, the current page may not have changed.

getOriginalUrl () gets the original URL for the current page. This is not always the same as the URL passed to WebViewClient.onPageStarted because although the load for that URL has begun, the current page may not have changed. Also, there may have been redirects resulting in a different URL to that originally requested.

like image 30
Linga Avatar answered Nov 06 '22 05:11

Linga