12

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!

1
  • 2
    can't you wrap it in a Url object and use the getHost() method? Commented Apr 16, 2014 at 20:36

1 Answer 1

43

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

Sign up to request clarification or add additional context in comments.

7 Comments

From where should I import Url?
look at doc ;) java.net.URL
This will give you malformed exception. Soln : stackoverflow.com/a/40405173/1137624
@cegprakash not if you catch it. Your proposed solution won't match many valid top level domains.
"Won't match man y valid top level domains..." Can someone please explain this or give an example? What domain name won't work with the code in this answer? And where does the 50% number come from for malformed URL's?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.