代码之家  ›  专栏  ›  技术社区  ›  Techboy

将字符串转换为Uri

  •  144
  • Techboy  · 技术社区  · 14 年前

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

    myUri=???;

    6 回复  |  直到 11 年前
        1
  •  455
  •   Lonzak    4 年前

    你可以用 parse 静态方法来自 Uri

    //...
    import android.net.Uri;
    //...
    
    Uri myUri = Uri.parse("http://stackoverflow.com")
    
        2
  •  42
  •   Lonzak    4 年前

    java.net .

    ...
    import java.net.URI;
    ...
    
    String myUrl = "http://stackoverflow.com";
    URI myURI = new URI(myUrl);
    
        3
  •  9
  •   Jeff Lockhart    6 年前

    如果您正在使用 科特林

    val uri = myUriString.toUri()
    

    添加Kotlin扩展( KTX公司 )将以下内容添加到应用程序模块的build.gradle

      repositories {
        google()
    }
    
    dependencies {
        implementation 'androidx.core:core-ktx:1.0.0-rc01'
    }
    
        4
  •  6
  •   JSON C11 Pardha Saradhi Survi    8 年前

    您可以使用 Uri.parse()

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

    注意: 机器人之间是有区别的 URI Uri .

        5
  •  0
  •   EricE    7 年前

    Java的语法分析器 java.net.URI 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
  •  -1
  •   Stuart Grimshaw    14 年前

    你打算用URI做什么?

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