4

I have gone through every related post I found here and still can't get it to work. I'm opening my WebView with a URL that has redirects (sometimes many redirects) and I want to know when the loading is really finished (URL finished loading with no more redirects).

So any suggestions?

Every post I read said to override shouldOverrideUrlLoading but no matter what I do shouldOverrideUrlLoading does not get called.

My code:

mWebView = (WebView) findViewById(R.id.webview);
WebSettings settings = mWebView.getSettings();
settings.setLoadWithOverviewMode(true);
settings.setJavaScriptEnabled(true);
settings.setLoadsImagesAutomatically(true);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setWebViewClient(new MyWebViewClient());
mUrl = "http://cdn.something.com/something.html";
//    mUrl = "https://www.google.com";
mWebView.loadUrl(mUrl);



public class MyWebViewClient extends WebViewClient {

    public MyWebViewClient() {}

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        Log.d(TAG, "WebViewClient: shouldOverrideUrlLoading");
        return false;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        Log.d(TAG, "WebViewClient: onPageStarted:  url: " + url);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        Log.d(TAG, "WebViewClient: onPageFinished: url: " + url);
        super.onPageFinished(view, url);
    }

}

Thank you

ELITE
  • 5,815
  • 3
  • 19
  • 29
Guy S
  • 1,424
  • 1
  • 15
  • 34
  • Did you try onLoadResource method? – Valentino Feb 09 '17 at 13:42
  • check [this](http://stackoverflow.com/questions/2659000/java-how-to-find-the-redirected-url-of-a-url) answer.. and you have to check if url redirect done or not in `onPageStarted` method of `WebViewClient`... – ELITE Feb 09 '17 at 13:42
  • @555 **never** edit source code just because you think "URL" should be in capital letters as **THAT BREAKS THE PROGRAM**. Please *stop* making questions worse by editing, especially if you're not perfect with the programming languages involved. *You're actively harming this community*. – Marcus Müller Feb 09 '17 at 14:36

2 Answers2

3

you are overriding the new shouldOverrideUrlLoading method which was added in android N,

you have to override both methods to make it work (old one which is deprecated and new one)

@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    final Uri uri = Uri.parse(url);
    return handleUri(uri);
}

@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    final Uri uri = request.getUrl();
    return handleUri(uri);
}

refer this ,and onPageFinished will be called when page has finished loading

Zaffy
  • 16,801
  • 8
  • 50
  • 77
Manohar
  • 22,116
  • 9
  • 108
  • 144
  • Thank you, this got shouldOverrideUrlLoading to fire, now I'll try using it to implement the suggestions i found. onPageFinished is called many times, my problem is finding out which is definitely the last time it is called. – Guy S Feb 09 '17 at 14:42
  • Also I want the redirects to be handled within the webview so the handleUri method is no good for me – Guy S Feb 09 '17 at 14:46
0

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.

jfcogato
  • 3,359
  • 3
  • 19
  • 19