我收到了一个错误:
W/System.err(32720): java.lang.IllegalArgumentException: Illegal character in query at index 89: https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f={%20mean0%22:%201}&apiKey=myApiKey
String apiURI = "https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f={%22mean0%22:%201}&apiKey=myApiKey";
我尝试过这样做 - 用%7B代替花括号:但它没有帮助
https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f=%7B"mean0":%201%7D&apiKey=myApiKey
任何?
编辑:
String query = "https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f={\""+arrayName+"\":%201}&apiKey=myApiKey";
try {
query = URLEncoder.encode(query, "utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String apiURI = query;
没有帮助。现在我得到了:
05-23 22:13:21.855: E/SendMail(12428): Target host must not be null, or set in parameters. scheme=null, host=null, path=https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f={"mean0":%201}&apiKey=myAPI
如果我将%20更改为查询声明中的空格,那么我得到:
05-23 22:14:51.435: E/SendMail(13164): Target host must not be null, or set in parameters. scheme=null, host=null, path=https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f={"mean0":+1}&apiKey=myAPI
另外如果我不在中间使用arrayName字符串,只是直接从浏览器使用字符串,效果是一样的!
答案 0 :(得分:5)
从我所看到的,每次尝试都会错过某些东西,编码不应该的东西,例如'?',或者对某些东西进行双重编码,从而对url编码中的'%'进行url编码。 / p>
如果只编码你关心转义的那一位,并完成一次呢?
String apiURI =
"https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f="
+ URLEncoder.encode("{\"mean0\": 1}", "UTF-8")
+ "&apiKey=myApiKey";
如果你想使用java.net.URI,你必须单独包含查询字符串,例如:
new URI(
"https",
"api.mongolab.com",
"/api/1/databases/activity_recognition/collections/entropy_data",
"f={\"mean0\": 1}&apiKey=myApiKey",
null
).toURL()
答案 1 :(得分:-1)
我猜你正在寻找像这样的东西
String flag1 = URLEncoder.encode("This string has spaces", "UTF-8");
您可以参考Oracle URL Encoder中的文档或参阅SOF
答案 2 :(得分:-1)
另一种方法是:
uri = new URI("https",
"api.mongolab.com",
"/api/1/databases/activity_recognition/collections/entropy_data?f={\"mean\": 1}&apiKey=myApiKey", null);
URL url = uri.toURL();
请注意,我将%22(urlencoded引号)更改为\“(转义引号),否则您的%符号将被urlencoded结束。
澄清一点,如果你这样做:
String query = "https://api.mongolab.com...";
query = URLEncoder.encode(query, "utf-8");
您最终将获得https%3A%2F%2Fapi.mongolab.com。