简体   繁体   中英

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. 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.

If you don't add www , some websites will still fail, for example, eurobeat-prime.com won't work without www prefix.

How can I process such links? (Because modern browsers do)

I don't think you need to append 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:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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