java.net.URLEncoder.encode(String) is deprecated, what should I use instead?

deprecated, java, network-programming, url

Solution

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()
  )
);

Problem

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?

Original source