最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Webview shouldOverrideUrlLoading not getting called - Stack Overflow

programmeradmin1浏览0评论

I am making an ebook reader which uses epub format to load books into webviews. In some of the books there is an anchor link to some portions in the same chapter. Each chapter is loaded as html. This is how the link look like

file:///storage/sdcard0/Android/data/com.abc.reader/files/Download/498935/epub/resources/498935/OEBPS/#footnote-165093-1-backlink

I tried using shouldOverrideUrlLoading() method to get the call back , but it's not getting called and when I press the links in onPageFinished the url shown as about:blank

reader.setWebViewClient(new WebViewClient() {


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.w("TESTTESTOVERRIDE "+url);
        view.loadUrl(url);
        return false;
    }


    @Override
    public void onPageFinished(WebView view, String url) {
        // after the data has been loaded, the following is executed
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

            System.out.println("check.... onPageFinishedEntered.."
                    + url.toString());

            view.loadUrl(jsfileloadurl);




    }

Any ideas?

EDIT: In 4.1 devices I get the anchor links correctly,but in 4.4 or 5.0 it is about:blank. (in both cases shouldOverrideUrlLoading is not called)

I am making an ebook reader which uses epub format to load books into webviews. In some of the books there is an anchor link to some portions in the same chapter. Each chapter is loaded as html. This is how the link look like

file:///storage/sdcard0/Android/data/com.abc.reader/files/Download/498935/epub/resources/498935/OEBPS/#footnote-165093-1-backlink

I tried using shouldOverrideUrlLoading() method to get the call back , but it's not getting called and when I press the links in onPageFinished the url shown as about:blank

reader.setWebViewClient(new WebViewClient() {


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.w("TESTTESTOVERRIDE "+url);
        view.loadUrl(url);
        return false;
    }


    @Override
    public void onPageFinished(WebView view, String url) {
        // after the data has been loaded, the following is executed
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

            System.out.println("check.... onPageFinishedEntered.."
                    + url.toString());

            view.loadUrl(jsfileloadurl);




    }

Any ideas?

EDIT: In 4.1 devices I get the anchor links correctly,but in 4.4 or 5.0 it is about:blank. (in both cases shouldOverrideUrlLoading is not called)

Share Improve this question edited Jul 30, 2015 at 5:29 shine_joseph asked Jul 29, 2015 at 13:35 shine_josephshine_joseph 2,9424 gold badges25 silver badges44 bronze badges 6
  • did you check it in log cat . that shouldOverrideUrlLoading is not getting called – Sourabh Bans Commented Jul 29, 2015 at 15:03
  • change it to ... return true; and than give it a try again.. – Sourabh Bans Commented Jul 29, 2015 at 18:18
  • Clicking anchor links does not cause any loading. The browser simply scrolls the content to show the link target. – Mikhail Naganov Commented Jul 29, 2015 at 20:41
  • ok...den how to do it. – shine_joseph Commented Jul 30, 2015 at 3:21
  • Have you set targetSdkVersion in your manifest? – Prasad Commented Aug 6, 2015 at 11:51
 |  Show 1 more comment

4 Answers 4

Reset to default 15 +25

I haven't tested this programmatically but I believe you are facing this issue because there was major changes in how webview works post OS 4.4 . You should check this link https://developer.android.com/guide/webapps/migrating.html#URLs

Under section 'Custom Url Handling' it says that shouldOverrideUrlLoading() will not be invoked for invalid url. Ideally file:// should be treated as valid url but seems like it's not happening here.

One possible solution is to load you main webview content with loadDataWithBaseURL and provide baseurl as some test url e.g. http://mytestap.testurl , it will guarantee shouldOverrideUrlLoading will get invoked all time. As a next step you need to remove prefix 'http://mytestap.testurl' if exist in the received url in shouldOverrideUrlLoading callback.

In my case it didn't work because of POST requests on web page. shouldOverrideUrlLoading:

Note: This method is not called for POST requests.

Note: This method may be called for subframes and with non-HTTP(S) schemes; calling WebView#loadUrl(String) with such a URL will fail.

Override shouldInterceptRequest instead (one or both versions). See also https://medium.com/@madmuc/intercept-all-network-traffic-in-webkit-on-android-9c56c9262c85.

Yes. Mr. androgeek answered it rightly. From Android OS 4.4(KK), if you implement callbacks such as shouldOverrideUrlLoading() or shouldInterceptRequest(), then WebView invokes them only for valid URLs. If you are using Custom URL and under your control then you need to follow RFC 3986 standard to above methods called. Kindly check RFC 3986 related file:// and correct your URL

I am not sure whether the below will resolve your problem or not.

Please add below code before setting the WebViewClient

reader.getSettings().setLoadWithOverviewMode(true);
reader.getSettings().setUseWideViewPort(true);

/*This makes the layout/page rendering independent of the devices. 
I use this to display local HTML pages.*/
reader.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL); 

In addition I have zoom controls enabled. Please note that I have tested my code from API-10 onwards with multiple devices and brands (HTC, Samsung, Nexus etc.) and found that the shouldOverrideUrlLoading works all the time.

If things do not work well, try extending the WebViewClient and Override the shouldOverrideUrlLoading method

class MyWebView extends WebViewClient{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false; //THis should be false always
    }
}

Now set the WebViewClient as reader.setWebViewClient(new MyWebView());

发布评论

评论列表(0)

  1. 暂无评论