如何在android中进行url编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3734844/
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
how to url encode in android?
提问by sivaraj
I am using grid view for displaying image using xml parsing,i got some exception like
我正在使用网格视图使用 xml 解析来显示图像,但出现了一些异常,例如
java.lang.IllegalArgumentException: Illegal character in path at index 80: http://www.theblacksheeponline.com/party_img/thumbspps/912big_361999096_FlickingOff Douchebag.jpg
java.lang.IllegalArgumentException:索引 80 处的路径中存在非法字符:http: //www.theblacksheeponline.com/party_img/thumbspps/912big_361999096_FlickingOff Douchebag.jpg
How to solve this problem? I want to display all kind of url,anybody knows please give sample code for me.
如何解决这个问题呢?我想显示所有类型的网址,任何人都知道请给我示例代码。
Thanks All
谢谢大家
回答by Cpt.Ohlund
URL encoding is done in the same way on android as in Java SE;
URL 编码在 android 上以与 Java SE 中相同的方式完成;
try {
String url = "http://www.example.com/?id=123&art=abc";
String encodedurl = URLEncoder.encode(url,"UTF-8");
Log.d("TEST", encodedurl);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
回答by Jedo
Also you can use this
你也可以用这个
private static final String ALLOWED_URI_CHARS = "@#&=*+-_.,:!?()/~'%";
String urlEncoded = Uri.encode(path, ALLOWED_URI_CHARS);
it's the most simple method
这是最简单的方法
回答by BitMask777
As Ben says in his comment, you should not use URLEncoder.encode to full URLs because you will change the semantics of the URL per the following example from the W3C:
正如 Ben 在他的评论中所说,您不应该使用 URLEncoder.encode 到完整的 URL,因为您将根据W3C的以下示例更改 URL 的语义:
The URIs http://www.w3.org/albert/bertram/marie-claudeand http://www.w3.org/albert/bertram%2Fmarie-claudeare NOT identical, as in the second case the encoded slash does not have hierarchical significance.
URI http://www.w3.org/albert/bertram/marie-claude和 http://www.w3.org/albert/bertram%2Fmarie-claude不相同,因为在第二种情况下编码的斜杠不相同不具有层次意义。
Instead, you should encode component parts of a URL independently per the following from RFC 3986Section 2.4
相反,您应该按照RFC 3986第 2.4 节中的以下内容独立编码 URL 的组成部分
Under normal circumstances, the only time when octets within a URI are percent-encoded is during the process of producing the URI from its component parts. This is when an implementation determines which of the reserved characters are to be used as subcomponent delimiters and which can be safely used as data. Once produced, a URI is always in its percent-encoded form.
在正常情况下,URI 中的八位字节被百分比编码的唯一时间是在从其组成部分生成 URI 的过程中。这是当实现确定哪些保留字符将用作子组件分隔符以及哪些可以安全地用作数据时。一旦生成,URI 总是采用百分比编码形式。
So, in short, for your case you should encode/escape your filename and then assemble the URL.
因此,简而言之,对于您的情况,您应该对文件名进行编码/转义,然后组合 URL。
回答by Dhruv Raval
You don't encode the entire URL, only parts of itthat come from "unreliable sources" like.
您不会对整个 URL进行编码,而只会对来自“不可靠来源”之类的部分内容进行编码。
String query = URLEncoder.encode("Hare Krishna ", "utf-8");
String url = "http://stackoverflow.com/search?q=" + query;
回答by Elango
you can use below method
您可以使用以下方法
public String parseURL(String url, Map<String, String> params)
{
Builder builder = Uri.parse(url).buildUpon();
for (String key : params.keySet())
{
builder.appendQueryParameter(key, params.get(key));
}
return builder.build().toString();
}
回答by Krunal Shah
I tried with URLEncoder that added (+) sign in replace of (" "), but it was not working and getting 404 url not found error.
我尝试使用添加 (+) 符号来替换 (" ") 的 URLEncoder,但它无法正常工作并出现 404 url not found 错误。
Then i googled for get better answer and found this and its working awesome.
然后我用谷歌搜索得到更好的答案,发现这个和它的工作很棒。
String urlStr = "http://www.example.com/test/file name.mp4";
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();
This way of encoding url its very useful because using of URL we can separate url into different part. So, there is no need to perform any string operation.
这种编码 url 的方式非常有用,因为使用 URL 我们可以将 url 分成不同的部分。因此,无需执行任何字符串操作。
Then second URI class, this approach takes advantage of the URI class feature of properly escaping components when you construct a URI via components rather than from a single string.
然后是第二个 URI 类,当您通过组件而不是从单个字符串构造 URI 时,此方法利用了正确转义组件的 URI 类功能。
回答by Oleg Smirnov
URLEncoder should be used only to encode queries, use java.net.URIclass instead:
URLEncoder 应仅用于对查询进行编码,请改用java.net.URI类:
URI uri = new URI( "http", "www.theblacksheeponline.com", "/party_img/thumbspps/912big_361999096_Flicking Off Douchebag.jpg", null); String request = uri.toASCIIString();
回答by Daniel Murphy
I recently wrote a quick URI encoder for this purpose. It even handles unicode characters. http://www.dmurph.com/2011/01/java-uri-encoder/
我最近为此编写了一个快速 URI 编码器。它甚至可以处理 unicode 字符。 http://www.dmurph.com/2011/01/java-uri-encoder/