Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get domain name from URL in Java/Android [duplicate]

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!

like image 912
hansottowirtz Avatar asked Apr 16 '14 20:04

hansottowirtz


People also ask

How to get the domain name from URL in java?

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.

What is domain name in Android Studio?

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.


Video Answer


1 Answers

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

like image 196
peshkira Avatar answered Nov 07 '22 08:11

peshkira