简体   繁体   中英

How to use URLEncoder with UTF-8

Documentation says I should use UTF-8 with URLEncoder .

I tried UTF-8. I got "cannot resolve symbol" error

URLEncoder.encode(str, "UTF_8");

I also tried this, but the API level is higher than I want it (level 19).

String UTF_8 = java.nio.charset.StandardCharsets.UTF_8.toString();

What is the proper and simple way to use this method with UTF-8?

The proper way is to spell it right. Try URLEncoder.encode(str, "UTF-8"); not with an underscore.

URLEncoder

This class is used to encode a string using the format required by application/x-www-form-urlencoded MIME content type.

All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9') and characters '.', '-', '*', '_' are converted into their hexadecimal value prepended by '%'. For example: '#' -> %23. In addition, spaces are substituted by '+'.

The recommended encoding scheme to use is UTF-8 .

Use

URLEncoder.encode(str, "UTF-8");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM