249

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

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

myUri = ???;

DisplayName
  • 3,093
  • 5
  • 35
  • 42
Techboy
  • 4,286
  • 5
  • 36
  • 45

8 Answers8

585

You can use the parse static method from Uri

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

Uri myUri = Uri.parse("http://stackoverflow.com")
Lonzak
  • 9,334
  • 5
  • 57
  • 88
ccheneson
  • 49,072
  • 8
  • 63
  • 68
  • 5
    I have tried this solution but i have a problem with it. First Uri need to be changed to URI(if it is from java.net package). Second it does not have the "parse" constructor. Do you have any idea what i am doing wrong? – Jürgen K. Sep 22 '15 at 14:53
  • 4
    There is a `Uri` class with a `parse` method http://developer.android.com/reference/android/net/Uri.html . There is also a `URI` class http://developer.android.com/reference/java/net/URI.html without a `parse` method. So the right class to use is `Uri`. Make sure you have the correct import – ccheneson Sep 22 '15 at 15:06
  • 2
    @ccheneson Why should Uri be more correct then URI? With URI you can just use the `create` method. It's as simple to use as Uri. So what benefit do you have from Uri? – kaba713 May 23 '17 at 15:22
  • That is right. Wrong imports are one of my annoying problems too. – Omid.N Mar 07 '20 at 10:06
  • So it is that simple :) – Mateusz Niedbal Apr 07 '21 at 20:07
54

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);
Lonzak
  • 9,334
  • 5
  • 57
  • 88
Jürgen K.
  • 3,427
  • 9
  • 30
  • 66
12

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'
}
Jeff Lockhart
  • 5,379
  • 4
  • 36
  • 51
Midhun Vijayakumar
  • 2,927
  • 2
  • 19
  • 23
7

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.

JSON C11
  • 11,272
  • 7
  • 78
  • 65
6
import java.net.URI;

Below also works for me :

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

OR

URI uri = new URI("http://stackoverflow.com");
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
2

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());
EricE
  • 74
  • 2
1

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

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");
Stuart Grimshaw
  • 1,541
  • 1
  • 18
  • 41