Android:无法在webview中传递网址

时间:2014-12-28 10:23:09

标签: android webview

我想在我的网页浏览中传递一个以www.example.com/xxxxxxx开头的网址,它可以代替xxxxxxx。看看你会理解的代码

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("http://www.example.com"+"*")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

我想从我的网站传递任何网址,否则在默认浏览器中打开它。

1 个答案:

答案 0 :(得分:0)

Uri.getHost()返回host from the authority,您应该尝试使用if (Uri.parse(url).getHost().equals("www.example.com"))if (Uri.parse(url).getHost().equals("example.com"))

另外,作为一种良好做法,您应该反转equals()这样的语句:"example.com".equals(Uri.parse(url).getHost()),因为您知道"example.com"永远不会是null,因此,永远不要抛出NullPointerException