以下是Cloud Code功能:
Parse.Cloud.define('testQuery', function(request, response) {
response.success('Test String');
});
我通过Parse Android SDK调用此函数:
try {
HashMap<String, Object> params = new HashMap<>();
ParseCloud.callFunctionInBackground("testQuery", params, new FunctionCallback<ArrayList<String>>() {
@Override
public void done(ArrayList<String> results, ParseException e) {
Log.d("Test", "Done");
}
});
} catch (ClassCastException e) {
Log.d("Test", "Exception: " + e.toString());
}
我知道我正在从String转换为ArrayList,这将导致ClassCastException。所以我添加了一个try-catch,但应用程序仍然崩溃。我无法捕捉到异常的原因是什么?感谢。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: app.honestly, PID: 23014
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.ArrayList
at app.myapp.MyActivity$1.done(MyActivity.java:189)
at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:115)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5234)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
答案 0 :(得分:0)
名称ParseCloud.callFunctionInBackground
表示callback
参数是异步执行的。输入try catch
,存储ParseCloud.callFunctionInBackground
和callback
,退出try catch
,然后在某个时间点执行callback
函数。
解决此问题的一种方法是接受String
,然后将其转换为数组列表:
ParseCloud.callFunctionInBackground("testQuery", params, new FunctionCallback<String>() {
@Override
public void done(String results, ParseException e) {
try {
List<String> result = convertToList(results);
}catch(Exception ex){
Log.e("Test", "Problem " + ex);
}
Log.d("Test", "Done");
}
});