Java 将字符串转换为 Uri

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3487389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:13:52  来源:igfitidea点击:

Convert String to Uri

javaandroidstringuri

提问by Techboy

How can I convert a String to a Uri in Java (Android)? i.e.:

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

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

myUri = ???;

myUri = ???;

采纳答案by ccheneson

You can use the parsestatic method from Uri

您可以使用parse静态方法Uri

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

回答by Stuart Grimshaw

What are you going to do with the URI?

你打算用 URI 做什么?

If you're just going to use it with an HttpGet for example, you can just use the string directly when creating the HttpGet instance.

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

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

回答by Jürgen K.

I am just using the java.netpackage. Here you can do the following:

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

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

回答by JSON C11

You can parse a String to a Uri by using Uri.parse()as shown below:

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

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

The following is an example of how you can use your newly created Uri in an implicit intent. To be viewed in a browser on the users phone.

以下是如何在隐式意图中使用新创建的 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);
}

NB:There is a difference between Androids URIand Uri.

注意:Androids URIUri之间存在差异。

回答by EricE

Java's parser in java.net.URIis going to fail if the URI isn't fully encoded to its standards. For example, try to parse: http://www.google.com/search?q=cat|dog. An exception will be thrown for the vertical bar.

java.net.URI如果 URI 没有完全按照其标准编码,Java 的解析器就会失败。例如,尝试解析:http://www.google.com/search?q=cat|dog. 垂直条将引发异常。

urllibmakes it easy to convert a string to a java.net.URI. It will pre-process and escape the URL.

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

回答by Midhun Vijayakumar

If you are using Kotlinand Kotlin android extensions, then there is a beautiful way of doing this.

如果您正在使用Kotlin和 Kotlin android 扩展,那么有一种很好的方法可以做到这一点。

val uri = myUriString.toUri()

To add Kotlin extensions (KTX) to your project add the following to your app module's build.gradle

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

  repositories {
    google()
}

dependencies {
    implementation 'androidx.core:core-ktx:1.0.0-rc01'
}