java.lang.ClassCastException:java.util.HashMap无法强制转换为com.parse.ParseObject

时间:2016-01-11 18:27:59

标签: java android json parse-platform

这是我的代码,用于请求短信接收的sendCode。当输入并提交号码时,它会崩溃,我仍然会通过短信接收代码。

ParseCloud.callFunctionInBackground("sendCode", params, new FunctionCallback<JSONObject>() {
                    public void done(JSONObject response, ParseException e) {
                        progressBar.setVisibility(View.GONE);
                        if (e == null) {
                            Log.d("Cloud Response", "There were no exceptions! " + response.toString());
                            codeUI();
                        } else {
                            Log.d("Cloud Response", "Exception: " + response.toString() + e);
                            Toast.makeText(getApplicationContext(),
                                    "Something went wrong.  Please try again." + e,
                                    Toast.LENGTH_LONG).show();
                            phoneNumberUI();
                        }
                    }
                });

这是我在Parse Cloud上的JSON文件

if (!phoneNumber || (phoneNumber.length != 10 && phoneNumber.length != 11)) return res.error('Invalid Parameters');
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query(Parse.User);
    query.equalTo('username', phoneNumber + "");
    query.first().then(function(result) {
        var min = 1000; var max = 9999;
        var num = Math.floor(Math.random() * (max - min + 1)) + min;

        if (result) {
            result.setPassword(secretPasswordToken + num);
            result.set("language", language);
            result.save().then(function() {
                return sendCodeSms(phoneNumber, num, language);
            }).then(function() {
                res.success({});
            }, function(err) {
                res.error(err);
            });

1 个答案:

答案 0 :(得分:0)

ParseCloud函数根据您的javascript返回HashMap层次结构,因此您可以使用以下代码。

ParseCloud.callFunctionInBackground("sendCode", params, new FunctionCallback<HashMap<String, Object>>() {
    public void done(HashMap<String, Object> response, ParseException e) {
        progressBar.setVisibility(View.GONE);
        if (e == null) {
            Log.d("Cloud Response", "There were no exceptions! " + response.toString());
            Log.d("Cloud Response", response.get("data").toString());
            codeUI();
        } else {
            Log.d("Cloud Response", "Exception: " + response.toString() + e);
            Toast.makeText(getApplicationContext(), "Something went wrong.  Please try again." + e, Toast.LENGTH_LONG).show();
            phoneNumberUI();
        }
    }
});

然后在你的javascript中你可以做出回应......

response.success({ data: "yay" });