有什么不对这个URI网址是什么?抛出:IllegalArgumentException,非法字符 [英] What is wrong with this URI URL? IllegalArgumentException, Illegal character

查看:1043
本文介绍了有什么不对这个URI网址是什么?抛出:IllegalArgumentException,非法字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个错误:

  W / System.err的(32720):java.lang.IllegalArgumentException异常:在指数89非法字符查询: https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f={%20mean0%22:%201}&apiKey=myApiKey
字符串apiURI = \"https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f={%22mean0%22:%201}&apiKey=myApiKey\";


  • 当我这个URI粘贴到浏览器正常工作。

  • 当我粘贴到浏览器中,打开它,然后复制URI回到我的code,它并不能帮助。

  • 指数89 { - 如何是非法字符

我试着这样做 - 以%7B替换大括号:但它并不能帮助

<$p$p><$c$c>https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f=%7B\"mean0\":%201%7D&apiKey=myApiKey

有人吗?


编辑:

 查询字符串= \"https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f={\\\"\"+arrayName+\"\\\":%201}&apiKey=myApiKey\";
    尝试{
        查询= URLEn coder.en code(查询,UTF-8);
    }赶上(UnsupportedEncodingException E1){
        // TODO自动生成catch块
        e1.printStackTrace();
    }
    字符串apiURI =查询;

于事无补。现在我越来越:

  05-23 22:13:21.855:E /的SendMail(12428):目标主机不能为空,或参数进行设置。方案= NULL,主机= 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):目标主机不能为空,或参数进行设置。方案= NULL,主机= NULL, path=https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f={\"mean0\":+1}&apiKey=myAPI

另外,如果我不使用字符串arrayName中在中间,只是使用字符串直接从浏览器,效果是一样的!


解决方案

这是我所看到的,每次尝试要么错过的东西,连接codeS的东西,它不应该,如?,或双-en codeS的东西,从而URL编码的URL编码的'%'。

如何只编码你关心逃脱位,而且做起来恰好一次?

 字符串apiURI =
    https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f=
    + URLEn coder.en code({\\mean0 \\:1},UTF-8)
    +与&amp; apiKey = myApiKey;

如果你想使用java.net.URI中,你必须单独包括查询字符串,例如:

 新的URI(
    https开头,
    api.mongolab.com
    / API / 1 /数据库/ activity_recognition /收藏/ entropy_data
    F = {\\mean0 \\:1}&安培; apiKey = myApiKey
    空值
  ).toURL()

I'm getting an error:

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";

  • When I paste this URI into the browser it works fine.
  • When I paste into browser, open it, and then copy the URI back into my code, it does not help.
  • Index 89 is { - how is that an illegal character?

I tried doing this - replacing curly brackets with %7B: but it does not help

https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f=%7B"mean0":%201%7D&apiKey=myApiKey

Anyone?


EDIT:

    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;

Does not help. Now I'm getting:

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

and if I change %20 to a space in the declaration of query then I'm getting:

 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

Also If I dont use the arrayName string in the middle and just use string straight from browser, effect is the same!

解决方案

From what I've seen, every attempt either misses something, encodes something it shouldn't, such as the '?', or double-encodes something, thereby url-encoding the '%' in the url encoding.

How about just encoding the bit you care about escaping, and doing it exactly once?

String apiURI =
    "https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f="
    + URLEncoder.encode("{\"mean0\": 1}", "UTF-8")
    + "&apiKey=myApiKey";

If you wanted to use java.net.URI, you'd have to include the query string separately, e.g.:

new URI(
    "https",
    "api.mongolab.com",
    "/api/1/databases/activity_recognition/collections/entropy_data",
    "f={\"mean0\": 1}&apiKey=myApiKey",
    null
  ).toURL()

这篇关于有什么不对这个URI网址是什么?抛出:IllegalArgumentException,非法字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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