尝试在Webview中打开在线PDF文件时,没有可用的预览消息

时间:2018-10-12 10:32:18

标签: android pdf webview

我的代码是这样的 WebView webview =新的WebView(getActivity());

    webview.getSettings().setJavaScriptEnabled(true);

   final ProgressDialog progDailog = ProgressDialog.show(getActivity(), "Loading", "Please wait...", true);
    progDailog.setCancelable(false);


    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setLoadWithOverviewMode(true);
    webview.getSettings().setUseWideViewPort(true);
    webview.getSettings().setPluginState(WebSettings.PluginState.ON);

// webview.setLayoutParams(新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT)); //以下几行将显示加载程序,直到下载pdf文件以供查看。         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();
        }
    });
    try {
        String urlEncoded = URLEncoder.encode(pdf, "UTF-8");
        pdf = "http://docs.google.com/viewer?url=" + urlEncoded;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
   String pdf = "http://192.168.2.154:8080/streamline/res/INV-000015.pdf";
    webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);

2 个答案:

答案 0 :(得分:1)

192.168.2.154是您本地网络上的计算机。 docs.google.com无法访问它。 docs.google.com只能访问公共IP地址上的公共URL。

也许您可能考虑使用other options for viewing PDFs

答案 1 :(得分:0)

这对我有用...我所做的是我在将URL与URL连接之前将URL解析为Uri,并且一切正常。下面是我的代码:

const int retryLimit = 3;
boolean success = false;
int retryCounter = 0;

while (!success
        && retryCounter++ < retryLimit) {
    try
    {
        // http request as is

        success = true; 
    }
    // Exception handling
}

希望有帮助。谢谢

相关问题