Nibha Jain
Nibha Jain

Reputation: 8141

java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to org.json.JSONObject while calling ParseCloud function

I am trying to call a function of parse cloud from android but keep getting ClassCastException. I gone through this thread , also tried the test function of accepted answer but that time also getting the same exception.

https://www.parse.com/questions/calling-cloud-function-that-returns-an-array-on-android

Any suggestion ,where am I wrong or how to call a function from Parse Cloud of Parse.com.

function in Parse Cloud looks like this :

Parse.Cloud.define("getMyNumber", function(request, response) {
  getSequence(function(sequence) {  
            if (sequence) {
              response.success({"Number":sequence});
            } else {
                response.error('Could not get a sequence.')
            }
        });
});

I am trying to call this from android :

ParseCloud.callFunctionInBackground("getMyNumber",
                 null, new FunctionCallback< Map<String, Object> >() { // line 182

                @Override
                public void done(Map<String, Object> arg0, ParseException arg1) {
                    // TODO Auto-generated method stub
                    String invoiceNo = arg0.get("Number").toString();
                    Log.d("TAG" ,arg0  + "invoice number");

                }
            });

logcat :

05-05 14:34:56.124: E/AndroidRuntime(17493): FATAL EXCEPTION: main
05-05 14:34:56.124: E/AndroidRuntime(17493): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ht.picturebook/com.ht.picturebook.HomeActivity}: java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to org.json.JSONObject
05-05 14:34:56.124: E/AndroidRuntime(17493):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at android.os.Looper.loop(Looper.java:137)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at android.app.ActivityThread.main(ActivityThread.java:4867)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at java.lang.reflect.Method.invokeNative(Native Method)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at java.lang.reflect.Method.invoke(Method.java:511)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at dalvik.system.NativeStart.main(Native Method)
05-05 14:34:56.124: E/AndroidRuntime(17493): Caused by: java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to org.json.JSONObject
05-05 14:34:56.124: E/AndroidRuntime(17493):    at com.parse.ParseCloud.constructCallCommand(ParseCloud.java:39)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at com.parse.ParseCloud.callFunctionAsync(ParseCloud.java:58)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at com.parse.ParseCloud.callFunction(ParseCloud.java:83)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at com.ht.picturebook.HomeActivity.findViews(HomeActivity.java:182)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at com.ht.picturebook.HomeActivity.onCreate(HomeActivity.java:121)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at android.app.Activity.performCreate(Activity.java:5047)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-05 14:34:56.124: E/AndroidRuntime(17493):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
05-05 14:34:56.124: E/AndroidRuntime(17493):    ... 11 more

Upvotes: 0

Views: 6042

Answers (2)

lxknvlk
lxknvlk

Reputation: 2850

I had a similar problem when in FunctionCallbact i was expecting a HashMap so i used generics new FunctionCallback<HashMap>() {}

But the function was returning something that was not castable to a hashmap, so just put Object instead of HashMap.

Upvotes: 0

Nibha Jain
Nibha Jain

Reputation: 8141

I changed this

ParseCloud.callFunctionInBackground("getMyNumber",
                 null, new FunctionCallback< Map<String, Object> >() { // line 182

                @Override
                public void done(Map<String, Object> arg0, ParseException arg1) {
                    // TODO Auto-generated method stub
                    String invoiceNo = arg0.get("Number").toString();
                    Log.d("TAG" ,arg0  + "invoice number");

                }
            });

to

ParseCloud.callFunctionInBackground("getMyNumber",
                new HashMap<String, Object>(), new FunctionCallback< Object >() {

                @Override
                public void done(Object arg0, ParseException arg1) {
                    // TODO Auto-generated method stub

                    String myInvoice = arg0.toString();
                    Log.d("TAG" , invoiceNumber + " : invoicenumber");
                }
            });

Upvotes: 2

Related Questions