Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use URLEncoder with UTF-8

Tags:

java

android

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?

like image 736
the_prole Avatar asked Mar 14 '23 20:03

the_prole


2 Answers

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

like image 78
Jan Avatar answered Mar 28 '23 08:03

Jan


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");
like image 24
IntelliJ Amiya Avatar answered Mar 28 '23 07:03

IntelliJ Amiya