Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've searched a few times now, but not come up with suitable answer to my problem...

Say I have a URL

http://example.com/query?q= 

and I have a query entered by the user such as: "random word £500 bank $"

I want the result to be a properly encoded URL:

http://example.com/query?q=random%20word%20%A3500%20bank%20%24

What's the best way to achieve this? I tried URLEncoder and creating URI/URL objects but none of them come out quite right.

share|improve this question
7  
What do you mean by "none of them come out quite right"? – Mark Elliot May 28 '12 at 14:12
@MarkElliot he probably means that the protocol and other slashes get encoded, not just the url path and query part. That was the problem for me too. I ended up with: uri = Uri.parse(url.replace(" ", "%20")) – Mārtiņš Briedis Mar 10 at 22:18
1  
I'd also check out this post stackoverflow.com/questions/724043/… as it discusses other solutions. – user2357809 May 7 at 9:56

4 Answers

up vote 98 down vote accepted

URLEncoder should be the way to go. You only need to keep in mind to encode only the individual query string parameter name and/or value, not the entire URL, for sure not the query string parameter separator character & nor the parameter name-value separator character =.

String url = "http://example.com/query?q=" + URLEncoder.encode("random word £500 bank $", "ISO-8859-1"); // Or "UTF-8".

Note that spaces in query parameters are represented by +, not %20, which is legitimately valid. The %20 is usually to be used to represent spaces in URI itself (the part before the URI-query string separator character ?), not in query string (the part after ?).

share|improve this answer
Isn't URLEncoder.encode deprecated? What are you using in it's stead? URLBuilder as Adam suggests below? – Piko May 10 at 17:21
1  
@Piko: Please read the Javadoc before making wild guesses. There are 2 encode() methods. The one mentioned in my answer is definitely not deprecated. – BalusC May 10 at 17:33
Is it safe to use "ISO-8859-1" as an encoding scheme even so the JavaDoc discourages it over "UTF-8"? – Roberto Linares May 16 at 15:51
@Roberto: I have no idea what javadoc you're talking about. – BalusC May 16 at 15:55
2  
@BalusC While what you say is technically true, in practice most servers will recognize only UTF-8, and it is in fact mandated by RFC3986; moreover, the very same Javadoc you linked says as much. You should change the answer to reflect this. – kbolino May 24 at 18:31
show 2 more comments

I would not use URLEncoder. Besides being incorrectly named (URLEncoder has nothing to do with URLs), inefficient (it uses a StringBuffer instead of Builder and does a couple of other things that are slow) Its also way too easy to screw it up.

Instead I would use URIBuilder or Spring's URIUtils or Commons Apache HttpClient. The reason being you have to escape the query parameters name (ie BalusC's answer q) differently than the parameter value.

The only downside to the above (that I found out painfully) is that URL's are not a true subset of URI's.

Since I'm just linking to other answers I marked this as a community wiki. Feel free to edit.

share|improve this answer
2  
BTW instead of -1 please say how I am wrong or its not helpful. I have seen countless times where people abuse URLEncoder so I was only trying to help. If its really not helpful I will delete my answer. – Adam Gent Apr 28 at 21:53
I'd say it was helpful...thanks for poiting to URIBuilder instead! – AlejandroVK May 6 at 21:41
@AdamGent: I agree, -1 without explanation is prob not a "good practice -1". I'd suggest you add an implementation of your answer to get back up in the points. – Adrien Be 2 days ago

this is additional not Particular Solution (Which is Already answered) if anyone wants to replace specific symbols or white space simply can use following :-

String strUrl =getResources().getString(R.string.ipaddress)
            + "/index.php?action=passengerRegistration&name=" + strname
            + "&gender=" + strgender + "&location=" + strLocation
            + "&phoneNo=" + strphoneno + "&password=" + strPwd + "&email="
            + stremail;

strRegUrl=strRegUrl.replaceAll(" ", "%20");

like your name containing white space "hello name"

than it will be "hello%20name" which will not cause problem !

similarly we target specific Symbol also ! :)

share|improve this answer

I would use something like:

String encodedurl = URLEncoder.encode(myUrl.toString(),"UTF-8"); 
share|improve this answer
14  
Encoding entire myUrl would also encode other entities, i.e. http:// would look like http%3A%2F%2F. @BalusC provided the right answer. – bgs Nov 28 '12 at 4:43

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.