【问题标题】:Convert String to Uri将字符串转换为 Uri
【发布时间】:2011-03-30 01:54:52
【问题描述】:

如何在 Java (Android) 中将字符串转换为 Uri?即:

String myUrl = "http://stackoverflow.com";

myUri = ???;

【问题讨论】:

    标签: java android string uri


    【解决方案1】:

    您可以使用来自Uriparse 静态方法

    //...
    import android.net.Uri;
    //...
    
    Uri myUri = Uri.parse("http://stackoverflow.com")
    

    【讨论】:

    • 我已经尝试过这个解决方案,但我遇到了问题。首先需要将 Uri 更改为 URI(如果它来自 java.net 包)。其次,它没有“解析”构造函数。你知道我做错了什么吗?
    • 有一个 Uri 类和一个 parse 方法 developer.android.com/reference/android/net/Uri.html 。还有一个 URIdeveloper.android.com/reference/java/net/URI.html 没有 parse 方法。所以要使用的正确类是Uri。确保您有正确的导入
    • @ccheneson 为什么 Uri 应该比 URI 更正确?使用 URI,您可以使用 create 方法。它和 Uri 一样简单易用。那么你从 Uri 中得到了什么好处呢?
    • 没错。错误的导入也是我恼人的问题之一。
    • 就这么简单:)
    【解决方案2】:

    我只是在使用java.net 。 在这里您可以执行以下操作:

    ...
    import java.net.URI;
    ...
    
    String myUrl = "http://stackoverflow.com";
    URI myURI = new URI(myUrl);
    

    【讨论】:

      【解决方案3】:

      如果您使用的是 Kotlin 和 Kotlin android 扩展,那么有一种很好的方法。

      val uri = myUriString.toUri()
      

      要将 Kotlin 扩展 (KTX) 添加到您的项目,请将以下内容添加到您的应用模块的 build.gradle

        repositories {
          google()
      }
      
      dependencies {
          implementation 'androidx.core:core-ktx:1.0.0-rc01'
      }
      

      【讨论】:

        【解决方案4】:

        您可以使用 Uri.parse() 将 String 解析为 Uri,如下所示:

        Uri myUri = Uri.parse("http://stackoverflow.com");
        

        以下示例说明了如何在隐式意图中使用新创建的 Uri。在用户手机的浏览器中查看。

        // Creates a new Implicit Intent, passing in our Uri as the second paramater.
        Intent webIntent = new Intent(Intent.ACTION_VIEW, myUri);
        
        // Checks to see if there is an Activity capable of handling the intent
        if (webIntent.resolveActivity(getPackageManager()) != null){
            startActivity(webIntent);
        }
        

        注意:URIUri之间存在差异。

        【讨论】:

          【解决方案5】:

          如果 URI 未完全按照其标准编码,java.net.URI 中的 Java 解析器将会失败。比如尝试解析:http://www.google.com/search?q=cat|dog。竖线会抛出异常。

          urllib 可以轻松地将字符串转换为java.net.URI。它将预处理和转义 URL。

          assertEquals("http://www.google.com/search?q=cat%7Cdog",
              Urls.createURI("http://www.google.com/search?q=cat|dog").toString());
          

          【讨论】:

            【解决方案6】:

            你也可以这样做

            对于http

            var response = await http.get(Uri.http("192.168.100.91", "/api/fetch.php"));
            

            对于 https

            var response = await http.get(Uri.https("192.168.100.91", "/api/fetch.php"));
            

            【讨论】:

              【解决方案7】:

              你打算用这个 URI 做什么?

              例如,如果您只是将它与 HttpGet 一起使用,则可以在创建 HttpGet 实例时直接使用该字符串。

              HttpGet get = new HttpGet("http://stackoverflow.com");
              

              【讨论】:

              • 但例如android.widget.ImageView.setImageURI 不会——所以这是一个完全有效的问题。
              • 其实这并没有回答问题
              【解决方案8】:
              import java.net.URI;
              

              以下内容也适用于我:

              URI uri = URI.create("http://stackoverflow.com");
              

              URI uri = new URI("http://stackoverflow.com");
              

              【讨论】:

                猜你喜欢
                • 2011-12-17
                • 2013-06-25
                • 2014-03-06
                • 2011-02-12
                • 2015-07-23
                • 2021-06-17
                • 1970-01-01
                • 1970-01-01
                • 2011-06-17
                相关资源
                最近更新 更多