As the title says: which encoder would give me space as %20
as opposed to +
? I need it for android. java.net.URLEncoder.encode gives +
Note that Java's URLEncoder class encodes space character( " " ) into a + sign. This is contrary to other languages like Javascript that encode space character into %20 .
Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format. URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.
The reserved characters are different for each part. For HTTP URLs, a space in a path fragment part has to be encoded to "%20" (not, absolutely not "+"), while the "+" character in the path fragment part can be left unencoded.
Android has it's own Uri
class which you could use.
E.g.
String url = Uri.parse("http://www.google.com").buildUpon()
.appendQueryParameter("q", "foo bar")
.appendQueryParameter("xml", "<Hellö>")
.build().toString();
results in
http://www.google.com?q=foo%20bar&xml=%3CHell%C3%B6%3E
Uri
Encodes characters in the given string as '%'-escaped octets using the UTF-8 scheme. Leaves letters ("A-Z", "a-z"), numbers ("0-9"), and unreserved characters ("_-!.~'()*") intact.
Note: only _-.*
are considered unreserved characters by URLEncoder
. !~'()
would get converted to %21%7E%27%28%29
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With