I get the following warning when using java.net.URLEncoder.encode:
warning: [deprecation] encode(java.lang.String)
in java.net.URLEncoder has been deprecated
What should I be using instead?
I get the following warning when using java.net.URLEncoder.encode:
warning: [deprecation] encode(java.lang.String)
in java.net.URLEncoder has been deprecated
What should I be using instead?
Use the other encode method in URLEncoder:
URLEncoder.encode(String, String)
The first parameter is the text to encode; the second is the name of the character encoding to use (e.g., UTF-8). For example:
System.out.println(
URLEncoder.encode(
"urlParameterString",
java.nio.charset.StandardCharsets.UTF_8.toString()
)
);
The usage of org.apache.commons.httpclient.URI is not strictly an issue; what is an issue is that you target the wrong constructor, which is depreciated.
Using just
new URI( [string] );
Will indeed flag it as depreciated. What is needed is to provide at minimum one additional argument (the first, below), and ideally two:
escaped: true if URI character sequence is in escaped form. false otherwise.charset: the charset string to do escape encoding, if
requiredThis will target a non-depreciated constructor within that class. So an ideal usage would be as such:
new URI( [string], true, StandardCharsets.UTF_8.toString() );
A bit crazy-late in the game (a hair over 11 years later - egad!), but I hope this helps someone else, especially if the method at the far end is still expecting a URI, such as org.apache.commons.httpclient.setURI().