What is wrong with this uri uri? IllegalArgumentException, Illegal Character

I get an error message:

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 insert this URI into the browser, it works fine.
  • When I insert into the browser, open it and then copy the URI into your code, this will not help.
  • Index 89 - {- How is it an illegal character?

I tried to do this - replacing curly braces with% 7B: but this does not help

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

Is 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 get:

 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 request declaration, I get:

  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 don't use the string arrayName in the middle and just use the string directly from the browser, the effect will be the same!

+4
source share
3 answers

From what I saw, each attempt either skips something, encodes something that it should not, for example, "?", Or encodes something double, thereby encoding the url "%" in url encoding .

How easy is it to encode a bit that you are interested in when shielding, and do 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 want to use java.net.URI, you have to include the query string separately, for example:

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

Another way to do this:

 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(); 

Please note that I changed% 22 (quotes from urlencoded) to \ "(escaped quotes), otherwise you will end your% sign, which will be urlencoded.

To clarify, the fact is that if you do this:

 String query = "https://api.mongolab.com..."; query = URLEncoder.encode(query, "utf-8"); 

you're done with https% 3A% 2F% 2Fapi.mongolab.com.

0
source

I think you are looking for something like this

 String flag1 = URLEncoder.encode("This string has spaces", "UTF-8"); 

You can link to documentation from Oracle URL Encoder or link to SOF

-1
source

Source: https://habr.com/ru/post/1482463/


All Articles