【问题标题】:How to add a protocol to a URL if it does not exist? (in an Android app)如果 URL 不存在,如何将协议添加到 URL? (在 Android 应用中)
【发布时间】:2015-05-29 09:03:11
【问题描述】:

我正在开发一个应用程序,该应用程序通过编辑文本小部件从用户那里获取 URL 链接。如何检查给定的 URL 是否有协议?如果没有,如何为特定 URL 添加正确的协议?

例如,如果用户输入:google.com 我怎样才能使它成为:https://google.com

主要问题是知道给定地址的正确 URL 协议(是http/https/ftp?等等)。

【问题讨论】:

标签: android


【解决方案1】:

您可以使用 String.startsWith() 来检查 url 字符串是否以 http:// 开头

public String valid_url(final String url)
    {
        if (!url.startsWith("http://") && !url.startsWith("https://"))
            {
               return "http://" + url;
            }
        return url;
    }

【讨论】:

  • || 在您的 if 条件错误。因为当 url 字符串看起来像 https://stackoverflow.com 并且你将它传递给这个函数时,结果将是:http://https://stackoverflow.com。用&& 替换|| 可以解决这个问题。
【解决方案2】:

首先使用 .contains() 方法检查 url 是否有协议 并使用 .indexof() 和 .substring() 方法获取协议

string url = editText.getText().toString();
string protocol;

if(url.contains("://")){
    //url has a protocol
    int index = url.indexof("://");
    //get protocol 
    protocol = url.substring(0,index-1);


}else{
    //url does not have a protocal
    // add your protocol to begining of the url
}

【讨论】:

    【解决方案3】:

    只需将您的输出字符串与.contains() 属性进行比较

     String value = editText.getText().toString();
    
     if(!value.contains("https://")) {
        // add https:// to ur string 
     }else {
         // No need to add 
     }
    

    【讨论】:

      【解决方案4】:

      这个解决方案对我有用:

      if(!url.startsWith("www.")&& !url.startsWith("http://") && !url.startsWith("https://")){
          url = "www."+url;
       }
      
      if(!url.startsWith("http://") && !url.startsWith("https://")){
          url = "http://"+url;
       }
      

      希望这会对你有所帮助。

      【讨论】:

        【解决方案5】:

        您可以使用 android web kit URLUTIL 类
        包android.webkit;

        URLUtil.guessUrl("your web address/String")
        

        示例场景:
        www.testurl.com
        testurl.com
        测试网址

        结果:
        http://www.testurl.com/

        【讨论】:

          【解决方案6】:

          如前所述,使用 SDK 的 URL 类。

          这里是一个例子:

          var urlWithScheme = new URL("https://www.google.com");
          var urlWithoutScheme = new URL("www.google.com");
          
          if (urlWithScheme.getProtocol() != null && urlWithScheme.getProtocol().length() > 0) {
              System.out.println("Given URL includes scheme: " + urlWithScheme.getProtocol());
          }
          
          if (urlWithoutScheme.getProtocol() != null && urlWithoutScheme.getProtocol().length() > 0) {
              System.out.println("Given URL includes scheme: " + urlWithoutScheme.getProtocol());
          } else {
              System.out.println("Url has no Protocol and you can't guess it by the domain name, because under this name all possible services can exist!");
          }
          

          如果给定的 URL 没有协议,你就猜不到。因为在一个域名下可以并行存在任何协议特定的服务。

          我会缩小范围,只支持 http 和 https。为此,您可以编写一个测试,例如连接到 https url,如果成功使用它,因为首选 https。如果你得到重定向或没有连接尝试 http ;)

          【讨论】:

            猜你喜欢
            • 2012-12-12
            • 2011-12-16
            • 2014-08-30
            • 1970-01-01
            • 2011-02-15
            • 2011-09-14
            • 1970-01-01
            • 1970-01-01
            • 2012-05-11
            相关资源
            最近更新 更多