Android: how to parse URL String with spaces to URI object?


I have a string representing an URL containing spaces and want to convert it to an URI object. If I simply try to create it via

String myString = " Name that has spaces inside.mp3";
URI myUri = new URI(myString);

it gives me Illegal character in path at index X

where index X is the position of the first space in the URL string.

How can i parse myString into a URI object?

3/5/2019 9:46:29 AM

Accepted Answer

You should in fact URI-encode the "invalid" characters. Since the string actually contains the complete URL, it's hard to properly URI-encode it. You don't know which slashes / should be taken into account and which not. You cannot predict that on a raw String beforehand. The problem really needs to be solved at a higher level. Where does that String come from? Is it hardcoded? Then just change it yourself accordingly. Does it come in as user input? Validate it and show error, let the user solve itself.

At any way, if you can ensure that it are only the spaces in URLs which makes it invalid, then you can also just do a string-by-string replace with %20:

URI uri = new URI(string.replace(" ", "%20"));

Or if you can ensure that it's only the part after the last slash which needs to be URI-encoded, then you can also just do so with help of utility class:

int pos = string.lastIndexOf('/') + 1;
URI uri = new URI(string.substring(0, pos) + Uri.encode(string.substring(pos)));

Do note that URLEncoder is insuitable for the task as it's designed to encode query string parameter names/values as per application/x-www-form-urlencoded rules (as used in HTML forms). See also Java URL encoding of query string parameters.

5/23/2017 12:02:48 PM, "utf-8");

This will URL-encode the string.

finalPartOfString is the part after the last slash - in your case, the name of the song, as it seems.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow