Para que serve a classe URLEncoder?
- Utility class for HTML form encoding;
- Contains static methods for converting a String to the MIME format.
Ao converter uma String, quais regras serão aplicadas?
- The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same;
- The special characters ".", "-", "*", and "_" remain the same;
- The space character " " is converted into a plus sign "".; // Por isso que temos Porto+Velho
- All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default encoding of the platform is used. // Por isso temos %C3%B4 na palavra Rondônia
URLEncoder possui o método encode, que:
- Translates a string into format using a specific encoding scheme;
- This method uses the supplied encoding scheme to obtain the bytes for unsafe characters. // No caso, foi utilizado o UTF-8
O método encode tem dois parâmetro:
- O primeiro será a String a ser convertida;
- O nome do codificador (encoding) que utilizaremos.
Voltando para a questão, podemos marcar a alternativa "C":
String querystring = "estado=" +
URLEncoder.encode("Rondônia", "UTF-8") + "&capital=" +
URLEncoder.encode("Porto Velho", "UTF-8");
Fonte: https://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html