4

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!

LucasSeveryn
  • 5,984
  • 8
  • 38
  • 65

3 Answers3

5

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()
  • FYI the correctly escaped URL seems to be `https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f=%7B%22mean0%22:%201%7D&apiKey=myApiKey` – Daniel Woffinden May 23 '13 at 23:12
-1

Another way to do this would be:

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

Note that I changed the %22 (urlencoded quotes) to \" (escaped quotes) otherwise you will wind up with your % sign being urlencoded.

To clarify, the point of this is that if you do this:

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

you will wind up with https%3A%2F%2Fapi.mongolab.com.

DevOfZot
  • 1,362
  • 1
  • 13
  • 26
  • Does not work still. It converts the URL to this `https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data%3Ff=%7B%22mean0%22:%201%7D&apiKey=myAPI` Now even if I paste it into browser it does not work. – LucasSeveryn May 23 '13 at 21:37
-2

I guess you are looking for something like this

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

You can refer to the documentation from Oracle URL Encoder or refer to SOF

Community
  • 1
  • 1
MDMalik
  • 3,951
  • 2
  • 25
  • 39
  • what do you write in the web-browser that you get it correct. – MDMalik May 23 '13 at 21:20
  • This: `https://api.mongolab.com/api/1/databases/activity_recognition/collections/entropy_data?f={"mean0":%201}&apiKey=myApiKey` – LucasSeveryn May 23 '13 at 21:26
  • Or this: `https://api.mongolab.com/api/1/databases/activity_recognition/collections/entro‌​py_data?f={%22"mean0":%201}&apiKey=myApiKey` – LucasSeveryn May 23 '13 at 21:27
  • Everytime I got this message "{ "message" : "Please provide a valid API key."}" Is your myApiKey initialized?? Just wondering – MDMalik May 23 '13 at 21:33
  • Well I did not want to share api key that allows modifications to my database. "myApiKey" is just a placeholder. – LucasSeveryn May 23 '13 at 21:40
  • I do understand that your are not sharing on SOF but hoping your are implementing on the code of yours. Thats what i meant – MDMalik May 23 '13 at 21:41
  • As I said, when I use the links from above post in the browser (with correct apikey) I am getting results I want. – LucasSeveryn May 23 '13 at 21:42