androiduriuribuilder

How to handle ports in URI.Builder class in Android


When testing my android app in development environment, I want to connect to a server running on port 9000. But when I supply the port to Builder.authority("localhost:9000"), it does not work. On the other hand if I create the same URL by hand like new URL("localhost:9000"), it works fine.

What is the alternative?


Solution

  • Uri.Builder will encode your URL so that ':' is replaced by %3 .

    To prevent encoding use the encoded versions of builder functions:

    String host = "localhost:9000";
    Uri.Builder builder = new Uri.Builder();
    builder.encodedAuthority(host);