【问题标题】:How I can change the particular URL into the sub URL如何将特定 URL 更改为子 URL
【发布时间】:2016-07-03 22:37:02
【问题描述】:

我有以下网址

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

我想把它分解成下面的 URL-

www.androidexample.com

如何为此编写代码?

【问题讨论】:

  • 我想在android webservice中实现这个
  • 为什么要中断......因为没有参数,webservice 将无法工作......

标签: java android


【解决方案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());

【讨论】:

    【解决方案2】:

    您必须获取特定 URL 的域名。只需在下面的代码中传递您的 URL,您就可以获得域或子 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;
    }
    

    【讨论】:

      【解决方案3】:

      用途:

      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();
      

      【讨论】:

        【解决方案4】:

        您可以在网址中查找// 和第一个/

        例如

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

        这里回答了:What is the fastest way to get the domain/host name from a URL?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-02-15
          • 1970-01-01
          • 1970-01-01
          • 2017-02-20
          • 1970-01-01
          • 2018-07-13
          • 1970-01-01
          相关资源
          最近更新 更多