简体   繁体   English

Android,Mandrill和Parse.com:Java ClassCastException:HashMap无法转换为类

[英]Android, Mandrill and Parse.com: Java ClassCastException: HashMap cannot be cast to a class

I don't know whether the problem is sourced this far back, so I will tell you about it anyway. 我不知道问题是否源于此,所以无论如何我都会告诉您。 I have a function stored as Parse Cloud Code which retrieves the user's list of scheduled emails, as per Parse and Mandrill API documentation. 我有一个存储为Parse Cloud Code的函数,该函数根据Parse和Mandrill API文档检索用户的预定电子邮件列表。 However, the returned object from Mandrill, according to their docs, is an array of structs containing String key-value pairs. 但是,根据Mandrill的文档,从其返回的对象是包含字符串键值对的结构数组。 You can see the relevant docs here: https://mandrillapp.com/api/docs/messages.JSON.html#method=list-scheduled 您可以在此处查看相关文档: https : //mandrillapp.com/api/docs/messages.JSON.html#method=list-scheduled

Parse Cloud Code can only be in Javascript, so how the returned array is interpreted by it, I am not sure. 解析云代码只能用Javascript编写,所以我不确定它如何解释返回的数组。 Either way, I have the cloud function return the returned array directly back to the Java method which called it in my Android application. 无论哪种方式,我都可以通过cloud函数将返回的数组直接返回给在我的Android应用程序中调用它的Java方法。 I thought it would arrive as a JSONArray, but that did not seem to be the case. 我以为它会以JSONArray的形式出现,但事实并非如此。 My editor, Android Studio, insisted that the return type was of ArrayList<ScheduledEmail> , and would not let me use any other parameter type, so I set it to that. 我的编辑器Android Studio坚持认为,返回类型为ArrayList<ScheduledEmail> ,并且不允许我使用任何其他参数类型,因此将其设置为该类型。 (ScheduledEmail is the class I created in Java to match the elements contained in a returned struct from Mandrill, for this method. Was this part of where I went wrong? How do I convert those structs in the returned array into Java-compatible key-value pairs?) However, when I try to access an object contained in the array, I receive this error: (ScheduledEmail是我在Java中创建的类,用于为此方法匹配Mandrill返回的结构中包含的元素。这是我出问题的地方吗?如何将返回数组中的这些结构转换为与Java兼容的键,值对?)但是,当我尝试访问数组中包含的对象时,出现此错误:

    java.lang.ClassCastException: java.util.HashMap cannot be cast to com.****.******.ScheduledEmail

Nowhere does there seem to have been a HashMap used. 似乎没有地方使用过HashMap。 What am I missing? 我想念什么? Should I have first done something to the returned array before having it sent back to the Java method? 我应该先对返回的数组做些什么,然后再将其发送回Java方法吗? Has Mandrill returned a HashMap? Mandrill是否已返回HashMap? If so, why would it be interpreted as the Java class which I intended it to match to? 如果是这样,为什么将它解释为我希望与之匹配的Java类?

The relevant portions of the Java method in question are as follows: 所讨论的Java方法的相关部分如下:

HashMap<String, Object> params = new HashMap<String, Object>();
params.put("recipient", "koolstr@gmail.com");
ParseCloud.callFunctionInBackground("listScheduledEmails", params, new FunctionCallback<ArrayList<ScheduledEmail>>() {
    public void done(ArrayList<ScheduledEmail> schedEmailsFromJS, ParseException e) {
        if (e == null) {
            for (int i=0; i < schedEmailsFromJS.size(); i++) {

                //This is the line which produces the error
                Log.d("sEmail", schedEmailsFromJS.get(i).get_id());
            }
        }
    }
});

This is the Parse Cloud Code Javascript function in which I retrieve the list of scheduled emails from Mandrill: 这是Parse Cloud Code Javascript函数,在其中我从Mandrill检索了已调度的电子邮件列表:

Parse.Cloud.define("listScheduledEmails", function(request, response) {
Parse.Cloud.httpRequest({
    method: 'POST',
    headers: {
        'Content-Type': 'application/json;charset=utf-8'
    },
    url: "https://mandrillapp.com/api/1.0/messages/list-scheduled.json",

    body: {
        key: "***************",
        to: request.params.recipient
    },
    success: function(httpResponse) {
        console.log("The scheduled emails have been successfully retrieved.");
        console.log(httpResponse.data);
        response.success(httpResponse.data);        
        console.log("Returned the list successfully.");
    },
    error: function(httpResponse) {
        console.error('Request failed with response code ' + httpResponse.status);
        response.error("Failed to retrieve messages.");
    }
});
});

Any help, ideas, clarifications, or criticisms would be appreciated. 任何帮助,想法,澄清或批评将不胜感激。 I've been stuck for half a day on this one problem and I can't seem to solve it. 我已经在这个问题上呆了半天,但似乎无法解决。 I don't know where I am going wrong. 我不知道我要去哪里错了。

You first need to figure out what type is passed down to the FunctionCallback : 您首先需要确定什么类型传递给FunctionCallback

ParseCloud.callFunctionInBackground("listScheduledEmails", params, new FunctionCallback<Object>() {
    public void done(Object o, ParseException e) {
        if (e == null) {
            Log.i("Tag", "Return type = " + o.getClass().getName());
        }
    }
});

After doing that, change your FunctionCallback to correctly handle the returned type, and, if needed, map it to your own bean type. 之后,更改您的FunctionCallback以正确处理返回的类型,并在需要时将其映射到您自己的bean类型。 I assume the returned type will be some sort of list of hash maps. 我假设返回的类型将是某种形式的哈希映射列表。

If that's indeed the case, you can change your implementation to something like this to get your list of ScheduledEmail objects: 如果确实如此,则可以将实现更改为以下内容,以获取ScheduledEmail对象的列表:

ParseCloud.callFunctionInBackground("listScheduledEmails", params, new FunctionCallback<List<Map>>() {
    public void done(List<Map> list, ParseException e) {
        if (e == null) {
            ArrayList<ScheduledEmail> scheduledEmails = new ArrayList<>();

            for(Map<String, ?> data : list) {
                ScheduledEmail scheduledEmail = new ScheduledEmail();
                scheduledEmail.set_id(data.get("_id"));
                // ... extract rest of data

                scheduledEmails.add(scheduledEmail);
            }
        }
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FlexJson 错误:ClassCastException:java.util.HashMap 无法转换为类 - FlexJson Error : ClassCastException: java.util.HashMap cannot be cast to class java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap 无法转换为模型类 android - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to model class android java.lang.ClassCastException:无法将java.util.HashMap强制转换为com.jms.testing.spring.InstructionMessage - java.lang.ClassCastException: java.util.HashMap cannot be cast to com.jms.testing.spring.InstructionMessage java.lang.ClassCastException:java.util.HashMap无法转换为自定义数据类 - java.lang.ClassCastException: java.util.HashMap cannot be cast to custom data class java.lang.ClassCastException: class java.util.Z063A5BC470661C3C7909 无法转换 - java.lang.ClassCastException: class java.util.HashMap cannot be cast : SpringBoot java.lang.ClassCastException: java.util.ArrayList 不能转换为 com.parse.ParseObject - java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.parse.ParseObject Java中的Parse.com REST API(非Android) - Parse.com REST API in Java (NOT Android) SDN4:ClassCastException:无法将java.util.HashMap强制转换为[EntityNode] - SDN4: ClassCastException: java.util.HashMap cannot be cast to [EntityNode] ClassCastException:无法将LinearLayout强制转换为(java类) - ClassCastException: LinearLayout cannot be cast to (java class) 将Java字符串数组或JSON字符串转换为Javascript数组(Parse.com云代码和Mandrill) - Convert Java string array or JSON string to Javascript Array (Parse.com Cloud Code and Mandrill)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM