1
votes

I've been using Parse's REST API for quite a while, but now in an attempt to reduce the amount of requests made to Parse's servers I've started exploring the Cloud Code features which looks quite nice -based on what I've read so far. The fact that you define and run some business logic on Parse's servers looks like a very powerful tool.

However, I haven't even been able to get the basics up and running. I followed this Getting Started Guide but I got stuck when trying to call the "hello" function on the background. Here's what I've done:

  1. Imported the jar files into the Android Studio project
  2. Install Parse's powershell command tool
  3. Created a cloud code directory locally. By default, it creates the hello function in "main.js"
  4. Deployed it
  5. Run a test on my Android app by call this hello function but it fails to parse the response

Here's the stacktrace of the error:

11-26 08:28:44.499  18299-18299/com.package.appname E/Leo_Debug﹕ Error: bad json response: org.json.JSONException: Value Invalid of type java.lang.String cannot be converted to JSONObject
com.parse.ParseException: bad json response: org.json.JSONException: Value Invalid of type java.lang.String cannot be converted to JSONObject
com.parse.ParseException: bad json response: org.json.JSONException: Value Invalid of type java.lang.String cannot be converted to JSONObject
        at com.parse.ParseRequest.connectionFailed(ParseRequest.java:415)
        at com.parse.ParseCommand.onResponse(ParseCommand.java:387)
        at com.parse.ParseCommand.onResponse(ParseCommand.java:36)
        at com.parse.ParseRequest$3.call(ParseRequest.java:295)
        at bolts.Task$2.run(Task.java:195)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)

This is the cloud code function I'm calling...

Parse.Cloud.define("hello", function(request, response) {
  response.success("Hello world!");
});

And this is how I'm making to the call to the function using the Parse SDK for Android...

String clientId = ctx.getString(R.string.parse_app_id);
String clientKey = ctx.getString(R.string.parse_app_api_key);
Map<String,Object> map = new HashMap<String, Object>();

Parse.initialize(ctx, clientId, clientKey);
ParseCloud.callFunctionInBackground("hello", map, new FunctionCallback<String>() {
        public void done(String o, ParseException e) {
            if(e != null) {
                Utils.LogError(e);
            }
            else{
                Utils.Log("ParseCloud.hello: " + o);
            }
        }
});

The ctx variable is an instance of the "Context" class and Utils it's just a helper class I used when debugging is turn on to print use info to the LogCat.

I haven't been able to find much information around neither on their old Forums Site nor on their dedicated Google Groups. If anyone ran into this issue before any help would be greatly appreciated

1
Instead of 'Hello World!', have you tried returning data in the json format? My first guess is that it is trying to parse the json and fails. - chris
can you please post json response ? - Haresh Chhelana
@chris thanks for the comment. Yeah, that was my first guess too. I tried passing both a plain json object and a "stringified" json object to the response.success function. I also tried changing the callback's generic type from a String to a JSONObject on the java code to no avail. The o parameter always comes null - Leo
@HareshChhelana if you look at the FunctionCallback class there's no way to get the full response - Leo
Some of my users are also experience problems like this. The response from Parse is null. Although checking the logs on Parse console, the response looks ok like with every other request and there are no errors and we've tested our cloud code but some of the users are facing an issue. It seems like a bug in the Parse Android SDK. Has anyone talked to the Parse devs about it? - Rahul Sainani

1 Answers

2
votes

Using Android Studio I was using compile 'com.parse:parse-android:1.+' which was perfectly fine until yesterday. After many attempts at all sorts, I found changing this to compile 'com.parse:parse-android:1.12 got rid of this error. So check your library references!