url – Uri Builder in android – '/' replaced by '%2F' and ":" is replaced by "%3A"-ThrowExceptions

Exception or error:

I want to build the following URI –

Builder builder = new Builder();
builder.appendPath(service + "/" +method);
return builder.build().toString();


  • URL_SCHEMEhttps
  • host10.112.88.182:8443/Vehicle/services/
  • servicesocialService
  • methodlogin

When this code runs I get the following URI –

/ is replaced by %2F and : is replaced by %3A

How to solve:

That’s how Uri.Builder works. It encodes non-safe URL characters with special meaning to their %xx hex values.

To prevent encoding URI parts that are already properly encoded, use the encoded versions of builder functions:

builder.appendEncodedPath(service + "/" +method);

But since all your URL parts are already ready and don’t need any further encoding, it’s easier to just use a regular StringBuilder to concatenate the parts.


Like laalto said, this is how Uri.Builder works but if you want to get the uri in a regular url form, like in your case:
you can do:

URL url = new URL(URLDecoder.decode(builder.build().toString(), "UTF-8"));


The solution is quite simple, simply use appendEncodedPath(), it will not encode your string, it just appends it as it is.

Leave a Reply

Your email address will not be published. Required fields are marked *