Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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 myStringinto a URI object?

share|improve this question

4 Answers 4

up vote 78 down vote accepted

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 android.net.Uri 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.

share|improve this answer
    
Replacing the spaces with %20 dit the trick. THX –  Mannaz Apr 7 '10 at 15:14
4  
@Mannaz - just be careful when another "invalid" symbol appears in a song name. –  Bozho Apr 7 '10 at 19:31
    
@BalusC i tried URLEncoder.encode("query string","UTF-8"); its returning with + symbol like this "query+string" where im expecting "%20". So i used string.replace with the hardcoded the values. Solved the issue. Thanks for the info. Is there any otherway to encode instead of manual replace..? –  praveenb Apr 5 '12 at 11:20
java.net.URLEncoder.encode(finalPartOfString, "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.

share|improve this answer
2  
It will also urlencode the colon and the slashes which would make the url still invalid. He basically only need to urlencode the spaces to get it valid. –  BalusC Apr 7 '10 at 14:26
    
@BalusC, thanks, I added an update. –  Bozho Apr 7 '10 at 14:29
    
Ok, this gets me by the URISyntaxException but now i get a 404 from the server. The url I get is http://myhost.com/media/mp3s/9/Agenda+of+swine+-+13.+Persecution+Ascension_+lea‌​ve+nothing+standing.mp3. I use the URI in an org.apache.http.client.methods.HttpGet.HttpGet Request. Any ideas? –  Mannaz Apr 7 '10 at 14:44
1  
@Bozho shure it is a client/encoding problem, because requesting the original URL (myString) in a normal Browser does not result in a 404 error. –  Mannaz Apr 7 '10 at 14:59
3  
I am using java.net.URLEncoder.encode("aa bb cc", "utf-8"); but instead of adding %20 instead of space it replacing +. "aa+bb+cc". Why this is happening. –  Sniper Oct 22 '13 at 14:54

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:

private Uri.Builder builder;
public Uri getUriFromUrl(String thisUrl) {
    URL url = new URL(thisUrl);
    builder =  new Uri.Builder()
                            .scheme(url.getProtocol())
                            .authority(url.getAuthority())
                            .appendPath(url.getPath());
    return builder.build();
}
share|improve this answer
URL url = Test.class.getResource(args[0]);  // reading demo file path from                                                   
                                            // same location where class                                    
File input=null;
try {
    input = new File(url.toURI());
} catch (URISyntaxException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
share|improve this answer
    
why negative marking? –  siddmuk2005 Aug 4 '14 at 10:26
    
because this isn't answering the question. –  MetaFight Sep 2 '14 at 15:56
    
I have given this for removing the space from URL so it solve my problem because while reading the file location FileInputStream points to null and while reading with null it will throw Exception bu using URI i didn't get the problem. –  siddmuk2005 Sep 4 '14 at 6:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.