java (Android) 中的 URLEncoder 编码 / URLDecoder 解码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/896721/
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
URLEncoder encode / URLDecoder decode in java (Android)
提问by Micha?l
I want to use the URLEncoder/URLDecoder class (java.net.URLEncoder/URLDecoder) in an application and the methods : encode(String s, String enc)/decode(String s, String enc), but I don't know what can be the value of the String argument enc? I want to encode/decode in the "x-www-form-urlencoded" MIME content type. Thank you for your help.
我想在应用程序中使用 URLEncoder/URLDecoder 类 (java.net.URLEncoder/URLDecoder) 和方法:encode(String s, String enc)/decode(String s, String enc),但我不知道是什么可以是字符串参数 enc 的值吗?我想在“x-www-form-urlencoded”MIME 内容类型中编码/解码。感谢您的帮助。
采纳答案by jgre
The encoding parameter is the character encoding you're using. For example "UTF-8"
.
encoding 参数是您使用的字符编码。For example "UTF-8"
.
回答by Sourabh
First you need to set the content-type as a 'x-www-form-urlencoded'. Then whatever content you would like to encode, encode it using "UTF-8".
首先,您需要将内容类型设置为“x-www-form-urlencoded”。然后,无论您想编码什么内容,都使用“UTF-8”对其进行编码。
For example:
例如:
For setting content to 'x-www-form-urlencoded':
将内容设置为“x-www-form-urlencoded”:
URL url = new URL("http://www.xyz.com/SomeContext/SomeAction"); <br>
URLConnection urlConnection = url.openConnection();<br>
....<br>
....<br>
urlConnection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
Or if you are using some JSP then you can write the following on top of it.
或者,如果您正在使用某些 JSP,那么您可以在其上编写以下内容。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><br>
< META http-equiv="Content-Type" content="text/html; charset=UTF-8">
< FORM action="someaction.jsp" enctype="application/x-www-form-urlencoded" name="InputForm" method="POST">
And to use URLEncoder:
并使用 URLEncoder:
String encodedString = URLEncoder.encode("hello","UTF-8");
回答by Stan
URLEncoder
and URLDecoder
both are exception Throwable
and thus must be at least enclosed by try/catch block. However there is a litle bit simplier way using android.net.Uri
:
URLEncoder
并且URLDecoder
两者都是异常Throwable
,因此必须至少包含在 try/catch 块中。然而,有一种更简单的方法使用android.net.Uri
:
Uri.decode(string);
Uri.encode(string);
Those are static methods, uses utf-8, available since API-1 and throws no exception.
这些是静态方法,使用 utf-8,自 API-1 以来可用,并且不会引发任何异常。
回答by Matthias Ronge
My personal favourite:
我个人的最爱:
static String baseNameFromURL(URL url) {
String shortName;
String path = url.getPath();
String escaped = path.substring(path.lastIndexOf('/') + 1);
try {
shortName = URLDecoder.decode(escaped, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new Error(e.getMessage(), e);
}
int period = shortName.lastIndexOf('.');
return period > -1 ? shortName.substring(0, period) : shortName;
}
Returns an empty String if the URL doesn't have a file name part, like https://stackoverflow.com/
or https://stackoverflow.com/questions/
. If there is a backslash in the file name part, it is conserved.
如果 URL 没有文件名部分,如https://stackoverflow.com/
或,则返回一个空字符串https://stackoverflow.com/questions/
。如果文件名部分有反斜杠,则保留。
You may strip off the last two lines if you need the short name with extension instead.
如果您需要带扩展名的短名称,您可以去掉最后两行。