1

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
4
  • Can you show your log cat? Commented May 5, 2015 at 9:23
  • 1
    @NibhaJain what is at line HomeActivity.java:182 Commented May 5, 2015 at 9:26
  • Check how did you import your JSONObject? Commented May 5, 2015 at 9:41
  • @NibhaJain ... org.json.JSONObject$1 cannot be cast to org.json.JSONObject seems that object is of type JSONObject$1 and its refer this object to JSONObject... and JSONObject$1 is not an actual class..its created at the time of deployement or runtime..so there may be some issue with your IDE..try to clean the project ..rebuild ..whatever IDE you are using... Commented May 5, 2015 at 9:42

2 Answers 2

1

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");
                }
            });
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.