3

I have an uri in the form of a string and i need to correctly acquire the path from it. Since I have a function that will correctly remove the query parameters from an android.net.Uri object I was thinking of simply converting my string path to a uri.

Is Uri.parse a valid way of converting a string to a uri? I've looked at a few examples, but none really explained whether or not this was a valid approach or if any additional parsing would have to be done.

I have already looked at this link How to convert a String to an android.net.Uri but it didn't really answer my question.

Thanks for any advice!

1
  • you mean method, not function Commented Jun 21, 2012 at 1:29

1 Answer 1

4

Yeah, you want to use Uri.parse(String). For example,

Uri uri = Uri.parse("content://com.example.provider.NotePad/notes/10");

To get the last segment, you would use

// id == "10"
String id = uri.getLastPathSegment();

To get the second to last segment, you would use

// notes == "notes"
String notes = uri.getPathSegments().get(0); 
Sign up to request clarification or add additional context in comments.

7 Comments

I guess this is a silly question, but this will also handle http requests just fine?
Well, Uri.parse parses a Uri. If you your HTTP requests are handled using the URL class, you won't be able to use Uri.parse. See this thread for more info.
Maybe I'm mixing my terms. I'm new at this. I have a string (if I copy and paste what's inside the quotes into the browser it will download a file. So this string is "http://..blah.." that's what I meant by http requests.).I want to remove its query parameters by converting it to an Uri. Based on what I've read and your comments, it seems like Uri.parse would work but I wasn't sure. Does that clarify my orginal question at all? My other problem is that I don't understand the difference between an Url and an Uri. Wiki didn't make sense to me :( Thanks so much for the help!
@user813375, A URL is a URI but a URI is not a URL. A URL is a specialization of URI that defines the network location of a specific representation for a given resource. A URI identifies a resource either by location or name. So basically the answer is yes, you can use Uri.parse on the string representation of a URL (but not the other way around).
@user813375, {all URLs} is a subset of {all URIs}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.