我尝试在 java 中执行 http 请求,但收到此错误:
java.lang.IllegalArgumentException:无效的 % 序列:索引 80 处的查询中的 %Ne:
try {
HttpClient Client = new DefaultHttpClient();
String URL = "Example.com";
HttpGet httpget = new HttpGet(URL);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String SetServerString = Client.execute(httpget, responseHandler);
Log.e("iets",SetServerString);
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
看来我不能在查询中使用字符“%”。但问题是我确实需要使用“%”,因为它代表我的 echonest.php 中的空格。
如何解决这个问题?
提前致谢
最佳答案
% 用于对 URL 中的特殊字符进行编码。要对实际的“%”字符进行编码,您需要使用 %25 网址将变为: http://bertenbevers.be/ProjectICT4/echonest.php?title=stormwatch&artist=Helvetic%25Nerds
有现成的库可以对 URL 进行编码。它将有助于避免其他奇怪的符号,如空格、斜杠等。
关于java - % 在 url http 请求 java 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29962383/