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.
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!
String urlString = "https://www.baeldung.com/java-tutorial"; URI uri = new URI(urlString); String host = uri. getHost(); Next, let's get a domain name using InternetDomainName class and its topPrivateDomain() method: InternetDomainName internetDomainName = InternetDomainName.
The domain name is used by Android Studio to generate a package name. Your package name is just a unique identifier for your application in the Google Play Store. It can be anything you want as long as it is unique. Generally, we use reverse domain names like com.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With