I have a string representing an URL containing spaces and want to convert it to an URI object. If is simple try to do
String myString = "http://myhost.com/media/mp3s/9/Agenda of swine - 13. Persecution Ascension_ leave nothing standing.mp3";
URI myUri = new URI(myString);
it gives me
java.net.URISyntaxException: 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?
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.I wrote this function:
To handle spaces, @, and other unsafe characters in arbitrary locations in the url path, Use Uri.Builder in combination with a local instance of URL as I have described here:
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 rawString
beforehand. The problem really needs to be solved at a higher level. Where does thatString
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
: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
android.net.Uri
utility class:Do note that
URLEncoder
is insuitable for the task as it's designed to encode query string parameter names/values as perapplication/x-www-form-urlencoded
rules (as used in HTML forms). See also Java URL encoding of query string parameters.