Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Uri.encode() in Android equivalent to encodeURIComponent() in Javascript?

I was trying to clarify difference between Java's URLEncoder.encode(), Javascript's encodeURI(), encodeURIComponent(), and Android's Uri.encode().

It looks like this:

  • Alphabets and numbers
    • Everyone keeps
  • .-*_
    • Everyone keeps
  • !~'()
    • URLEncoder.encode() encodes, others keep
  • ,/?:@&=+$#
    • encodeURI() keeps, others encode
  • Space
    • + for URLEncoder.encode(), %20 for others

It seems like URLEncoder.encode() and encodeURIComponent() behaves the same. Am I correct, or in fact they also have some difference?

like image 394
Romulus Urakagi Ts'ai Avatar asked Oct 29 '25 18:10

Romulus Urakagi Ts'ai


1 Answers

Interesting question. I just ran some code to test this:

Javascript encodeURIComponent

encodeURIComponent escapes all characters except:

Not Escaped: A-Z a-z 0-9 - _ . ! ~ * ' ( )

Code:

var sb = [];
for (var i = 0; i < 256; ++i) {
    var encoded = encodeURIComponent(String.fromCharCode(i));
    if (encoded.indexOf('%') !== 0 && !encoded.match(/^[a-zA-Z0-9]+$/)) {
        sb.push(encoded);
    }
}
console.log(sb.join(' '));

Result:

! ' ( ) * - . _ ~

Javascript encodeURI

encodeURI escapes all characters except:

Not Escaped: A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; , / ? : @ & = + $ #

Code:

var sb = [];
for (var i = 0; i < 256; ++i) {
    var encoded = encodeURI(String.fromCharCode(i));
    if (encoded.indexOf('%') !== 0 && !encoded.match(/^[a-zA-Z0-9]+$/)) {
        sb.push(encoded);
    }
}
console.log(sb.join(' '));

Result:

! # $ & ' ( ) * + , - . / : ; = ? @ _ ~

Java URLEncoder.encode

Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme. This method uses the supplied encoding scheme to obtain the bytes for unsafe characters.

Code:

public static void main(String[] args) {
    try {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 256; ++i) {
            String encoded = URLEncoder.encode(String.valueOf((char) i), "UTF-8");
            if (!encoded.startsWith("%") && !encoded.matches("^[a-zA-Z0-9]+$")) {
                sb.append(' ').append(encoded);
            }
        }
        System.out.println(sb.substring(1));
    } catch (Exception e) {}
}

Result:

Note that + is actually a whitespace.

+ * - . _

Android Uri.encode

Encodes characters in the given string as '%'-escaped octets using the UTF-8 scheme. Leaves letters ("A-Z", "a-z"), numbers ("0-9"), and unreserved characters ("_-!.~'()*") intact. Encodes all other characters.

Code:

try {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 256; ++i) {
        String encoded = Uri.encode(String.valueOf((char) i));
        if (!encoded.startsWith("%") && !encoded.matches("^[a-zA-Z0-9]+$")) {
            sb.append(' ').append(encoded);
        }
    }
    System.out.println(sb.substring(1));
} catch (Exception e) {}

Result:

! ' ( ) * - . _ ~
like image 194
Yeti Avatar answered Oct 31 '25 09:10

Yeti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!