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

前端 未结 6 1858
佛祖请我去吃肉
佛祖请我去吃肉 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:53

    Use the class URLEncoder:

    URLEncoder.encode(String s, String enc)
    

    Where :

    s - String to be translated.

    enc - The name of a supported character encoding.

    Standard charsets:

    US-ASCII Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set ISO-8859-1 ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1

    UTF-8 Eight-bit UCS Transformation Format

    UTF-16BE Sixteen-bit UCS Transformation Format, big-endian byte order

    UTF-16LE Sixteen-bit UCS Transformation Format, little-endian byte order

    UTF-16 Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark

    Example:

    import java.net.URLEncoder;
    
    String stringEncoded = URLEncoder.encode(
        "This text must be encoded! aeiou áéíóú ñ, peace!", "UTF-8");
    

提交回复
热议问题