I'm trying to run my custom cloud code function from my Android app. I feel like it's something weird and I think that's not sufficiently documented in the guides of the platform.
Please help me, I am loosing way too much time over this!
The custom cloud code function returns an array of objects that looks like this:
[
{
"status":false,
"usr":{
"URL":"a string",
"__type":"Object",
"blacklisted":false,
"className":"_User",
"createdAt":"2015-09-28T08:22:09.266Z",
"description":"a string",
"email":"a string",
"emailVerified":true,
"facebookId":"a string",
"first_name":"a string",
"gender":"male",
"last_name":"a string",
"name":"Filippo",
"objectId":"a string",
"updatedAt":"2015-09-28T12:58:31.455Z",
"username":"a string"
}
}
// Some other objects...
]
Now I'm trying to call the cloud code function from Android client with this:
final HashMap<String, ParseObject> params = new HashMap<String, ParseObject>();
ParseCloud.callFunctionInBackground("getConversations", params, new FunctionCallback<ArrayList<JSONObject>>() {
@Override
public void done(final ArrayList<JSONObject> results, ParseException e) {
myAdapter = new InboxAdapter(getActivity(), results);
listview.setAdapter(myAdapter);
}
}
I get some strange behavior though, since using ArrayList<JSONObject>
as generic for the callback won't allow me to populate an adapter, which keeps returning java.lang.ClassCastException: java.util.HashMap cannot be cast to org.json.JSONObject
in the getItem()
method
private ArrayList<JSONObject> list;
private Context ctx;
public InboxAdapter(Context c, ArrayList<JSONObject> s) {
ctx = c;
list = s;
}
/* All other methods */
@Override
public JSONObject getItem(int position) {
return list.get(position); // Gives exception!
}
I find it really strange since it's an ArrayList of JSONObject
s and shouldn't care about maps and so on.
Also trying to use new FunctionCallback<JSONArray>() {}
bounces me back, because it requires an arraylist! (and to be honest I didn't figure out why).
So what should I do in order to handle such a response from the Android side? Is there something I have to do in order to correctly parse the results?
Thanks in advance.
hi can you modify your getConversations() written in cloud..Pass a Json Object in response.success...Here is a working example where test is your getConversations() function :-
Parse.Cloud.define("test", function(request, response) {
var text = "hello world";
var jsonObject = {
"answer": text
};
response.success(jsonObject);
});
Then call it and get values from android like this :-
ParseCloud.callFunctionInBackground("test", null, new FunctionCallback< Map<String, Object> >() {
public void done(Map<String, Object> mapObject, ParseException e) {
if (e == null){
Toast.makeText(appContext, mapObject.get("answer").toString(), Toast.LENGTH_LONG).show();
}
}
});