2

I need to do some queries against my datastore in Java but I can't seem to get the parameters syntax right. I tried like this:

String params = "?Active=1";
String urlString = "https://api.parse.com/1/classes/Cars" + params;

Or as per the document here:

String params = "where={Active:1}";

But both ways generate an exception.

If I don't do the query and simply try to get all the objects with this request string:

String urlString = "https://api.parse.com/1/classes/Cars"

everything works fine. So the problem is definitely the params sequence. So is there a way to do Prase.com rest queries in Java?

EDIT: adding the exception string in response to a request from the first comment:

java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.parse.com/1/classes/Cars?where={Active:1}
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1838)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)

I should also note that when I use the regular http syntax, as in

params = "?Dealer=asdf";

the query comes back with all the objects, as if the parameter wasn't there.

Eddy
  • 3,533
  • 13
  • 59
  • 89
  • Please paste the exception and maybe the code where the query is sent. – Jean-François Savard Sep 06 '14 at 15:20
  • @Jean-FrançoisSavard I added it to the question. – Eddy Sep 06 '14 at 17:30
  • Have you tried running the queries manually? Maybe you missed part of their API – Dave Sep 06 '14 at 17:33
  • @Dave how do you mean manually? – Eddy Sep 06 '14 at 17:36
  • By opening a browser or some http tool and opening that url, to look at the result. E.g. click here: https://api.parse.com/1/classes/Cars?where={Active:1} and notice that it asks for authorisation. Maybe that's what you forgot to include? – Dave Sep 06 '14 at 17:37
  • No, that's not it. I tried putting in my my app id and rest key but it just asks for authentication again. – Eddy Sep 06 '14 at 17:44

1 Answers1

2

Here are a couple of working examples for the params string:

String params = "where={\"objectId\":\"ldl49l3kd98\"}";

String params = "where={\"CompanyName\":\"BMW\", \"Price\":{\"$gte\":29000,\"$lte\":49000}}";

And if you need non English characters, like I do, encode the param string like this:

params = URLEncoder.encode(params, "UTF-8");
Eddy
  • 3,533
  • 13
  • 59
  • 89
  • Where would we add the parameters into the request? Would we access those parameters in cloud code by request.params? – JMStudios.jrichardson Jul 02 '15 at 15:15
  • @JMStudios.jrichardson you add these parameters in your application code. Then you get them in the cloud by calling request.params.ParamName. – Eddy Jul 03 '15 at 12:07
  • @Eddy can you please post an example of adding a username parameter to a httlpurlconnection object? I am having a lot of trouble with the parameters. Would you rather I make a new post? – JMStudios.jrichardson Jul 06 '15 at 17:26
  • @Eddy here is the question I posted. http://stackoverflow.com/questions/31252869/java-passing-parameters-to-parse-com-cloud-code-function – JMStudios.jrichardson Jul 06 '15 at 18:40
  • Hi, sorry, I haven't touched this in a while. Hopefully someone from Parse.com would take a look at it. Good luck! – Eddy Jul 07 '15 at 17:36