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.
01.
Uri.Builder b = Uri.parse(
"http://www.yoursite.com:12345"
).buildUpon();
02.
03.
b.path(
"/path/to/something/"
);
04.
b.appendQueryParameter(
"arg1"
, String.valueOf(
42
));
05.
06.
if
(username !=
""
) {
07.
b.appendQueryParameter(
"username"
, username);
08.
}
09.
10.
String url = b.build().toString();
And there you have it, a customisable URL with support for port number and queries.