将字符串转换为 Uri

发布于 2024-09-14 13:43:06 字数 149 浏览 3 评论 0原文

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

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

myUri = ???;

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

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

myUri = ???;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

厌倦 2024-09-21 13:43:06

您可以使用 Uri 中的 parse 静态方法

//...
import android.net.Uri;
//...

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

You can use the parse static method from Uri

//...
import android.net.Uri;
//...

Uri myUri = Uri.parse("http://stackoverflow.com")
[浮城] 2024-09-21 13:43:06

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

...
import java.net.URI;
...

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

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

...
import java.net.URI;
...

String myUrl = "http://stackoverflow.com";
URI myURI = new URI(myUrl);
淡淡绿茶香 2024-09-21 13:43:06

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

val uri = myUriString.toUri()

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

  repositories {
    google()
}

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

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

val uri = myUriString.toUri()

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

  repositories {
    google()
}

dependencies {
    implementation 'androidx.core:core-ktx:1.0.0-rc01'
}
素食主义者 2024-09-21 13:43:06
import java.net.URI;

以下也适用于我:

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

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

Below also works for me :

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

OR

URI uri = new URI("http://stackoverflow.com");
睫毛溺水了 2024-09-21 13:43:06

您可以使用 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);
}

注意: Android 之间存在差异 URI Uri

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

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.

// 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 URI and Uri.

青朷 2024-09-21 13:43:06

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

Java's parser in java.net.URI is 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.

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

assertEquals("http://www.google.com/search?q=cat%7Cdog",
    Urls.createURI("http://www.google.com/search?q=cat|dog").toString());
来日方长 2024-09-21 13:43:06

您也可以

对 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"));

you can do this too

for http

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

or

for https

var response = await http.get(Uri.https("192.168.100.91", "/api/fetch.php"));
忆梦 2024-09-21 13:43:06

您打算如何处理 URI?

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

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

What are you going to do with the 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 get = new HttpGet("http://stackoverflow.com");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文