Search code examples
javaandroidcachingwebviewcustom-component

why Android Webview sometimes does not display anything?


I need to open a PDF file from URL like this

So I'm using Webview, but sometimes (most of the time) does not display anything.

Here is my code:


        Url = "https://docs.google.com/viewer?url=" + "https://www.tutorialspoint.com/android/android_tutorial.pdf";

        webView.getSettings().setJavaScriptEnabled (true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                progDailog.show();
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, final String url) {
                progDailog.dismiss();
            }
        });
        webView.loadUrl(Url);

Shat should I do?


Solution

  • You can implement the onReceivedError method in your WebViewClient to handle errors.

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        // Handle error
        Log.e("WebViewError", "Error: " + description);
    }
    

    You can also try to use Google Docs Viewer directly.

    Url = "https://docs.google.com/gview?embedded=true&url=" + "https://www.tutorialspoint.com/android/android_tutorial.pdf";