【问题标题】:Checking Uri if they starts with "http://" or "https://"检查 Uri 是否以“http://”或“https://”开头
【发布时间】:2014-11-17 04:36:40
【问题描述】:

我正在检查字符串是否以“http://”或“https://”开头。我想在开头添加“http://”,但它们不是。不过,

String str = "www.stackoverflow.com"
str = "http://" + str;

会导致

str = "http//stackoverflow.com"

中间的“:”不见了。我该怎么办?

【问题讨论】:

  • 我不明白: 怎么不见了?它不应该。
  • 正在尝试剥离 www 并添加 http(s) ?

标签: android uri startswith


【解决方案1】:

我使用“android.webkit.URLUtil”类来检查 URL。但它有其自身的局限性。

我检测到它允许在 url 中使用空格。

// if url contains http://

 if(url.contains("http://")){
    // Validate URL
    if (!URLUtil.isValidUrl(url)) {

        Toast.makeText(getActivity(),"Invalid URL specified", Toast.LENGTH_SHORT).show();

        return;

         }else{ 

         // Your URL is Ready   

        }                       
         }else{// if url does not contains http://

        url = "http://" + url;  //add it to yur url 

         // Validate URL
        if (!URLUtil.isValidUrl(url)) { 
         Toast.makeText(getActivity(),"Invalid URL specified", Toast.LENGTH_SHORT).show();
             return;
            }else{ 
                    // Your link is ready   

                    }               
        }

URLUtil at Android developer

Stack Over flow Answer how to validate a URL / website name in EditText in Android?

【讨论】:

    【解决方案2】:
    if (URLUtil.isNetworkUrl(requestUrl)) {
               // it means url start with http or https
            }else{
          // do relevant action
         }
    

    【讨论】:

      【解决方案3】:
      String str = "www.stackoverflow.com"
      str = "http://" + str;
      

      上面的结果只是预期的,即“http://www.stackoverflow.com” 你能发布你的确切代码你在那里做什么。

      【讨论】:

        猜你喜欢
        • 2016-04-10
        • 2011-05-24
        • 1970-01-01
        • 2012-07-03
        • 2016-08-25
        • 2011-06-06
        • 1970-01-01
        • 2012-02-06
        相关资源
        最近更新 更多