Convert String to Uri

JavaAndroidStringUri

Java Problem Overview


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

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

myUri = ???;

Java Solutions


Solution 1 - Java

You can use the parse static method from Uri

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

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

Solution 2 - Java

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

Solution 3 - Java

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'
}

Solution 4 - Java

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.

Solution 5 - Java

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

Solution 6 - Java

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

Solution 7 - Java

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

Solution 8 - Java

import java.net.URI;

Below also works for me :

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

OR

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTechboyView Question on Stackoverflow
Solution 1 - JavacchenesonView Answer on Stackoverflow
Solution 2 - JavaJürgen K.View Answer on Stackoverflow
Solution 3 - JavaMidhun VijayakumarView Answer on Stackoverflow
Solution 4 - JavaJSON C11View Answer on Stackoverflow
Solution 5 - JavaEricEView Answer on Stackoverflow
Solution 6 - Javabhupesh shakyaView Answer on Stackoverflow
Solution 7 - JavaStuart GrimshawView Answer on Stackoverflow
Solution 8 - JavaShubham JainView Answer on Stackoverflow