Q:

Android(java) equilavent for NSString stringByAddingPercentEscapesUsingEncoding:?

I'm looking for the equivalent of the stringByAddingPercentEscapesUsingEncoding method in Java (for Android).
I tried URLEncoder.encode() but it doesn't do the same.
I don't expect "/" or ":" to be "percent escaped" but "œ" (for example) should be.
Any ideas?
Edit:
I just don't want http:// to become http%3a%2f%2f

A:

Well if you want to leave : and / untouched, I think you need to do it yourself, because that's not valid percent-encoding and therefore there is little chance for a solution already existing in java. I also checked the documentation for stringByAddingPercentEscapesUsingEncoding and it doesn't behave like that either.
Comparing documentation, URLEncoder.encode(string, encoding) is exactly the same as the NS method, so your requirement is indeed weird. Here's the custom code anyway:
public static String stringByAddingPercentEscapesUsingEncoding( String input, String charset ) throws UnsupportedEncodingException {
    byte[] bytes = input.getBytes(charset);
    StringBuilder sb = new StringBuilder(bytes.length);
    for( int i = 0; i < bytes.length; ++i ) {
        int cp = bytes[i] < 0 ? bytes[i] + 256 : bytes[i];
        if( cp <= 0x20 || cp >= 0x7F || (
            cp == 0x22 || cp == 0x25 || cp == 0x3C ||
            cp == 0x3E || cp == 0x20 || cp == 0x5B ||
            cp == 0x5C || cp == 0x5D || cp == 0x5E ||
            cp == 0x60 || cp == 0x7b || cp == 0x7c ||
            cp == 0x7d
            )) {
            sb.append( String.format( "%%%02X", cp ) );
        }
        else {
            sb.append( (char)cp );
        }
    }
    return sb.toString();
}

public static String stringByAddingPercentEscapesUsingEncoding( String input ) {
    try {
        return stringByAddingPercentEscapesUsingEncoding(input, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Java platforms are required to support UTF-8");
        // will never happen
    }
}

public static void main(String[] args) {
    System.out.println(
        stringByAddingPercentEscapesUsingEncoding("http://en.wikipedia.org/wiki/œäsd")
    );
    //http://en.wikipedia.org/wiki/%C5%93%C3%A4sd

}

