简体   繁体   English

在没有http:// www前缀的WebView中加载URL

[英]Load URL in WebView without http://www prefix

Please, don't mark this as duplicate. 请不要将其标记为重复。

If you call WebView.loadUrl with a parameter like google.com , it will fail. 如果使用google.com参数调用WebView.loadUrl ,则会失败。 So, as other similar questions suggest, you do something like this: 因此,正如其他类似问题所示,您可以这样做:

if(!url.startsWith("www."))
    url = "www." + url;
if(!url.startsWith("http://") && !url.startsWith("https://"))
    url = "http://" + url;
webView.loadUrl(url);

But, in case of, say, play.google.com , it will try to load http://www.play.google.com and fail. 但是,如果是play.google.com ,则会尝试加载http://www.play.google.com并失败。

If you don't add www , some websites will still fail, for example, eurobeat-prime.com won't work without www prefix. 如果您不添加www ,某些网站仍然会失败,例如,如果没有www前缀, eurobeat-prime.com将无法运行。

How can I process such links? 我该如何处理这些链接? (Because modern browsers do) (因为现代浏览器这样做)

I don't think you need to append www 我认为你不需要附加www

You can directly use this instead. 您可以直接使用它。

if(!url.startsWith("http://") && !url.startsWith("https://"))
    url = "http://" + url;
webView.loadUrl(url);

if you run across a URL that won't open in the webView directly, you can usually succeed in opening it in an external browser window: 如果您遇到无法直接在webView中打开的URL,您通常可以成功在外部浏览器窗口中打开它:

protected void handleExternalDeviceActivity(String url) {
    //pass this special URL to an external activity (outside of the app)
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(intent);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM