java.net.URLEncoder.encode(String) 已弃用,我应该改用什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/213506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.net.URLEncoder.encode(String) is deprecated, what should I use instead?
提问by Frank Krueger
I get the following warning when using java.net.URLEncoder.encode
:
使用时我收到以下警告java.net.URLEncoder.encode
:
warning: [deprecation] encode(java.lang.String) in java.net.URLEncoder has been deprecated
What should I be using instead?
我应该用什么代替?
采纳答案by Will Wagner
Use the other encode
method in URLEncoder:
使用URLEncoder 中的另encode
一种方法:
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:
第一个参数是要编码的文本;第二个是要使用的字符编码的名称(例如,UTF-8
)。例如:
System.out.println(
URLEncoder.encode(
"urlParameterString",
java.nio.charset.StandardCharsets.UTF_8.toString()
)
);
回答by Atul Darne
You should use:
你应该使用:
URLEncoder.encode("NAME", "UTF-8");
回答by user3591718
The first parameter is the String to encode; the second is the name of the character encoding to use (e.g., UTF-8).
第一个参数是要编码的字符串;第二个是要使用的字符编码的名称(例如,UTF-8)。
回答by htafoya
As an additional reference for the other responses, instead of using "UTF-8"you can use:
至于其他的反应,而不是使用,额外的参考“UTF-8”你可以使用:
HTTP.UTF_8
HTTP.UTF_8
which is included since Java 4 as part of the org.apache.http.protocol library, which is included also since Android API 1.
它自 Java 4 起作为 org.apache.http.protocol 库的一部分包含在内,自 Android API 1 起也包含在内。
回答by Jorgesys
Use the class URLEncoder:
使用类URLEncoder:
URLEncoder.encode(String s, String enc)
Where :
在哪里 :
s- String to be translated.
enc- The name of a supported character encoding.
s- 要翻译的字符串。
enc- 支持的字符编码的名称。
Standard charsets:
标准字符集:
US-ASCIISeven-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-8Eight-bit UCS Transformation Format
UTF-16BESixteen-bit UCS Transformation Format, big-endian byte order
UTF-16LESixteen-bit UCS Transformation Format, little-endian byte order
UTF-16Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark
US-ASCII七位 ASCII,又名 ISO646-US,又名 Unicode 字符集的基本拉丁块 ISO-8859-1 ISO 拉丁字母 1 号,又名 ISO-LATIN-1
UTF-8八位 UCS 转换格式
UTF-16BE十六位 UCS 转换格式,大端字节序
UTF-16LE十六位 UCS 转换格式,小端字节序
UTF-16十六位 UCS 转换格式,由可选字节顺序标记标识的字节顺序
Example:
例子:
import java.net.URLEncoder;
String stringEncoded = URLEncoder.encode(
"This text must be encoded! aeiou áéíóú ?, peace!", "UTF-8");
回答by R. K?bis
The usage of org.apache.commons.httpclient.URI
is not strictly an issue; what is an issue is that you target the wrong constructor, which isdepreciated.
的使用org.apache.commons.httpclient.URI
不是严格的问题;问题是您针对错误的构造函数,该构造函数已折旧。
Using just
仅使用
new URI( [string] );
Will indeed flag it as depreciated. What is needed is to provide at minimum one additional argument (the first, below), and ideally two:
确实会将其标记为折旧。需要的是提供至少一个额外的参数(第一个,下面),最好是两个:
escaped
: true if URI character sequence is in escaped form. false otherwise.charset
: the charset string to do escape encoding, if required
escaped
: 如果 URI 字符序列是转义形式,则为真。否则为假。charset
: 如果需要,要进行转义编码的字符集字符串
This will target a non-depreciated constructor within that class. So an ideal usage would be as such:
这将针对该类中的非折旧构造函数。所以理想的用法是这样的:
new URI( [string], true, StandardCharsets.UTF_8.toString() );
A bit crazy-late in the game (a hair over 11 years later - egad!), but I hope this helps someone else, especially if the method at the far end is stillexpecting a URI, such as org.apache.commons.httpclient.setURI()
.
在游戏后期有点疯狂(11 年后的头发 - egad!),但我希望这对其他人有所帮助,特别是如果远端的方法仍然需要 URI,例如org.apache.commons.httpclient.setURI()
.