Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WebView在android中多次加载url_Android - Fatal编程技术网

WebView在android中多次加载url

WebView在android中多次加载url,android,Android,android中的webview在加载url时多次加载。 下面是代码 public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.contains(".pdf")) { String[] spliturl = url.split("http://someurl/");

android中的webview在加载url时多次加载。 下面是代码

 public boolean shouldOverrideUrlLoading(WebView view, String url)
            {

                if (url.contains(".pdf")) {
                    String[] spliturl = url.split("http://someurl/");
                    String googleurl = "http://docs.google.com/viewer?embedded=true&url=";
                    System.out.println("Google Url"+googleurl);
                    System.out.println("spliturl"+spliturl[1]);
                     view.loadUrl(googleurl+spliturl[1]);
                }
                else
                     view.loadUrl(url);

                return true;
            }
        });
我正在拆分url,因为它包含多个url,要在google document viewer上传递以呈现pdf文档。 第一次正确拆分url时,url被连接到google docs中打开,但webview通过在拆分url处提供ArrayIndexOutOfBoundsException再次执行[1]。 有人能告诉我为什么这会再次发生吗。
谢谢。

您应该始终检查数组的大小是否超过请求的索引:

if (url.contains(".pdf") && url.split("http://someurl/").size()>2){
// your code
}

不知道为什么会调用它-可能是多次重定向。

我不知道为什么会多次调用它,但解决方案是在onPageStarted中处理它,而不是在shouldOverrideUrlLoading中处理它

    boolean calledOnce=false;

public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);

        return true;
    }

public void onPageStarted(WebView view, String url, Bitmap favicon) {
    if (url.contains(".pdf") && !calledOnce) {
            String[] spliturl = url.split("http://someurl/");
            String googleurl = "http://docs.google.com/viewer?embedded=true&url=";
            System.out.println("Google Url"+googleurl);
            System.out.println("spliturl"+spliturl[1]);
            url = googleurl+spliturl[1];
            calledOnce = true;
        }       
    super.onPageStarted(view, url, favicon);
}

如果您直接在导航器中打开pdf,可能会更简单:String googleDocsUrl=“;Intent Intent Intent=new Intent(Intent.ACTION_视图);Intent.setDataAndType(Uri.parse(googleDocsUrl),“text/html”);startActivity(Intent);