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.

- 10,070
- 23
- 89
- 150

- 11,530
- 9
- 46
- 48
-
2Possible duplicate of [Android WebView, how to handle redirects in app instead of opening a browser](http://stackoverflow.com/questions/4066438/android-webview-how-to-handle-redirects-in-app-instead-of-opening-a-browser) – Tomasz Dzieniak Jul 08 '16 at 08:18
4 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
}
});

- 1,481
- 9
- 10
-
-
3There are other methods that you could override in WebViewClient. **onLoadResource** and **onPageFinished**. Probably you can catch your url in one of them. – jDur Nov 07 '13 at 11:54
-
I did but it seems that the web view always returns the original url. – a fair player Nov 07 '13 at 11:56
-
well, Thank you. it turned out that i was using a wrong url. any way your answer deserves to be marked as correct. – a fair player Nov 07 '13 at 12:11
-
3while searching for a solution for the same problem I found [an article](http://www.catchingtales.com/android-webview-shouldoverrideurlloading-and-redirect/416/) which says that **shouldOverrideUrlLoading** method does not get invoked for redirections in Android ver < 3 (SDK 11). They advise to use **onPageStarted** as a workaround and it worked for me. However, I didn't check the bug exists indeed. – remarkov Mar 29 '14 at 10:39
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.

- 10,379
- 10
- 52
- 104
-
7Actually, I need the changed url NOT the original one. what always happens is that I'm getting the original one. – a fair player Nov 07 '13 at 09:59
In my case WebViewClient wasn't showing if there was changes on the webview, I supposed that is something about the web that is been running.
I could get that information from the WebChromeClient with OnProgressChanged, I don't know if this would help people anyway, but here is the code:
webview.webChromeClient = object : WebChromeClient(){
override fun onProgressChanged(view: WebView?, newProgress: Int) {
super.onProgressChanged(view, newProgress)
if (newProgress == 100) {
Log.d("testing", webview.getOriginalUrl())
Log.d("testing", webview.url)
}
}
}
This way I could know what is been loaded and when is finished.
You must get the onPageFinished
method with javascript interface
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:window.Android.onUrlChange(window.location.href);");
super.onPageFinished(view, url);
}
});
public class WebAppInterface {
Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void onUrlChange(String url) {
Toast.makeText(getApplicationContext(), url, Toast.LENGTH_SHORT).show();
}
}

- 2,324
- 26
- 22
- 31

- 1
- 1