java.net.URLEncoder.encode encodes space as + but I need %20

android, java, url-encoding, urlencode

Solution

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`.

Problem

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 `+`

Original source

Related problems