Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webview shouldOverrideUrlLoading not getting 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)

like image 880
shine_joseph Avatar asked Jul 29 '15 13:07

shine_joseph


2 Answers

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.

like image 76
androgeek Avatar answered Oct 12 '22 16:10

androgeek


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.

like image 3
CoolMind Avatar answered Oct 12 '22 16:10

CoolMind