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

前端 未结 6 1865
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 21:08

I get the following warning when using java.net.URLEncoder.encode:

warning: [deprecation] encode(java.lang.String)
         in java.net.URLEncoder has         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 21:49

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

提交回复
热议问题