Tuesday, January 25, 2011

Android: Create a URL using Uri.Builder().build() with port numbers

Uri.Builder.build() works quite well with normal URLs, but it fails with port number support.

The easiest way that I discovered to make it support port numbers was to make it parse a given URL first then work with it.

Uri.Builder b = Uri.parse("http://www.yoursite.com:12345").buildUpon();

b.path("/path/to/something/");
b.appendQueryParameter("arg1", String.valueOf(42));

if (username != "") {
b.appendQueryParameter("username", username);
}

String url = b.build().toString();

And there you have it, a customisable URL with support for port number and queries.

2 comments:

Leave your thoughts ...

---
If you are having trouble with copy/pasting in comments, you need to sign in or click 'Preview'.

For more information about this Firefox bug, see here.