从 Java/Android 中的 URL 获取域名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23119617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get domain name from URL in Java/Android
提问by hansottowirtz
In my app, I need to get the favicon.ico from a URL. (eg. "http://google.com/favicon.ico"). Users can input all kinds of URLs, and I need only the domain name.
在我的应用程序中,我需要从 URL 获取 favicon.ico。(例如“ http://google.com/favicon.ico”)。用户可以输入各种网址,我只需要域名。
Examples:
例子:
http://drive.google.com/bla/bla/bla -> drive.google.com
www.facebook.com/lol -> www.facebook.com
192.168.0.1 -> 192.168.0.1 (but not really necessary)
Does anyone have a method to get this? Thanks!
有没有人有办法得到这个?谢谢!
采纳答案by peshkira
Try using something like
尝试使用类似的东西
String u = "www.facebook.com/lol";
URL url = new URL(u);
String host = url.getHost(); // should be www.facebook.com
If you need different parts of the Url look at the documentation and the other getters here: http://developer.android.com/reference/java/net/URL.html
如果您需要 Url 的不同部分,请查看此处的文档和其他 getter:http: //developer.android.com/reference/java/net/URL.html