If your URL contains some characters that are not valid in the URLs, you should use a URL encoded string. They seem to do this in Java using a URI .
new URI( "http", null, "abc.com", 123, "abc].mp3", null, null).toURL().getPort();
If you already have a URL string:
URL url = new URL("http://abc.com:123/abc].mp3");
Then this works for me:
new URI( url.getProtocol(), null, url.getHost(), url.getPort(), url.getPath(), null, null);
But then again, I'm using url.getPort() , which, as you said, does not work. But when I test on Java 6 now. new URL("http://abc.com:123/abc].mp3").getPort(); really works for me, maybe it's just on android, it doesn't work? If this does not work, I think it is better to use a third-party library for this. The Apache Http client, which is included in Android, seems to have extra functionality for URLs: see org.apache.http.client.utils
See also URL Encoding of an HTTP Address in Java
Jonas source share