在Android的URL编码 [英] URL encoding in Android

查看:222
本文介绍了在Android的URL编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么连接codeA 网址在Android的?

How do you encode a URL in Android?

我以为是这样的:

final String encodedURL = URLEncoder.encode(urlAsString, "UTF-8");
URL url = new URL(encodedURL);

如果我做了上述情况,的http:// urlAsString 替换 HTTP%3A%2F%2F 连接codedURL ,然后我得到一个 java.net.MalformedURLException 当我使用的URL。

If I do the above, the http:// in urlAsString is replaced by http%3A%2F%2F in encodedURL and then I get a java.net.MalformedURLException when I use the URL.

推荐答案

您没有连接$ C C完整的URL $,只有来自可靠来源它的一部分。

You don't encode the entire URL, only parts of it that come from "unreliable sources".

String query = URLEncoder.encode("apples oranges", "utf-8");
String url = "http://stackoverflow.com/search?q=" + query;

另外,你可以使用<一个href="https://github.com/yanchenko/droidparts/blob/master/droidparts/src/org/droidparts/util/Strings.java">Strings.urlEn$c$c(String STR)的 DroidParts 不抛出checked异常。

Alternatively, you can use Strings.urlEncode(String str) of DroidParts that doesn't throw checked exceptions.

或者使用类似

String uri = Uri.parse("http://...")
                .buildUpon()
                .appendQueryParameter("key", "val")
                .build().toString();

这篇关于在Android的URL编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆