gpt4 book ai didi

java - 网址非法字符

转载 作者:太空宇宙 更新时间:2023-11-03 11:44:35 24 4
gpt4 key购买 nike

这是我的代码:

HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android");
HttpGet request = new HttpGet();
request.setHeader("Content-Type", "text/plain; charset=utf-8");
Log.d("URL", convertURL(URL));
request.setURI(new URI(URL));
HttpResponse response = client.execute(request);
bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");

我不知道我的 URL 中的哪个错误:

http://localhost/CyborgService/chatservice.php?action=recive_game&nick_sender=mkdarkness&pass=MV030595&date_last=2012-11-18 09:46:37&id_game=1

我已经使用了一个函数来转换 URL,但是没有用。但是,如果我尝试在浏览器中打开此 URL,它会成功打开。

这是我的错误:

11-18 21:46:37.766: E/GetHttp(823): java.net.URISyntaxException: Illegal character in query at index 127: http://192.168.0.182/CyborgService/chatservice.php?action=recive_game&nick_sender=mkdarkness&pass=MV030595&date_last=2012-11-18 09:46:37&id_game=1

最佳答案

您的网址中有一个空格,位置127。日期生成为“date_last=2012-11-18 09:46:37”,导致打开网址时出错。

空格在 URL 中未被正式接受,尽管您的浏览器会很乐意将其转换为“%20”或“+”,这两种空格在 URL 中的有效表示。您应该转义所有字符:您可以用“+”替换空格或只通过 URLEncoder 传递字符串并完成它。

要使用 URLEncoder,请参阅例如this question : 仅使用 URLEncoder 编码参数值,而不是完整的 URL。或者使用具有几个参数的 URI 的构造函数之一,而不是单个参数。您没有显示构建 URL 的代码,因此我无法对其进行明确评论。但是如果你有一个参数映射 parameterMap 它将是这样的:

String url = baseUrl + "?";
for (String key : parameterMap.keys())
{
String value = parameterMap.get(key);
String encoded = URLEncoder.encode(value, "UTF-8");
url += key + "&" + encoded;
}

改天我们可以讨论为什么Java需要设置编码,然后要求编码是“UTF-8”,而不是仅仅使用“UTF-8”作为默认编码,但现在这段代码应该做这个把戏。

关于java - 网址非法字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13445055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com