1

I have the following URL

http://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65&aaid=90

I want to break this into below URL-

www.androidexample.com

How can write code for this?

soccer7
  • 3,547
  • 3
  • 29
  • 50
pawan kumar
  • 61
  • 10

4 Answers4

1

Use:

String uriStr = "http://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65&aaid=90";
String host = new URI(uriStr).getHost();
Alexey
  • 1,198
  • 1
  • 15
  • 35
1
URI uri = new URI("http://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65&aaid=90");
    System.out.println("URI      : " + uri);
    System.out.println(uri.getHost());
Bajirao Shinde
  • 1,356
  • 1
  • 18
  • 26
0

You can look for the // and for the first / in the url.

e.g.

int slashslash = url.indexOf("//") + 2;
domain = url.substring(slashslash, url.indexOf('/', slashslash));

It was answered here: What is the fastest way to get the domain/host name from a URL?

Community
  • 1
  • 1
Someone
  • 560
  • 9
  • 21
0

You have to get domain name of particular URL. just pass your URL in below code you can get domain or sub URL.

String url="http://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65&aaid=90";
String subUrl = getDomainName(url);


public static String getDomainName(String url) throws URISyntaxException {
URI uri = new URI(url);
String domain = uri.getHost();
return domain.startsWith("www.") ? domain.substring(4) : domain;
}
Ravi Vaghela
  • 3,420
  • 2
  • 23
  • 51