这是我的代码:
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");
我不知道我的网址中有哪个错误:
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,则会成功打开.
这是我的错误:
06001
解决方法:
您的URL中有一个空格,位置127.日期生成为“date_last = 2012-11-18 09:46:37”,这会在打开URL时导致错误.
URL中没有正式接受空格,尽管您的浏览器会很乐意将其转换为“”或“”,这两者都是URL中空格的有效表示.你应该转义所有字符:你可以用“”替换空格,或者只是通过URLEncoder传递字符串并完成它.
要使用URLEncoder,请参阅例如this question:仅使用URLEncoder编码参数值,而不是完整的URL.或者使用URI的构造函数之一,它具有一些参数,而不是一个参数.您没有显示构造URL的代码,因此我无法明确地对其进行评论.但是如果你有一个参数mapMap的地图,它将是这样的:
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,url,android,uri
来源: https://codeday.me/bug/20190718/1494350.html