如何返回数组或其它集合元素从PhoneGap的Android插件类型 [英] How to return a array or other collections elements type from phonegap android plugin
本文介绍了如何返回数组或其它集合元素从PhoneGap的Android插件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里是我的测试code在一个Java插件的一部分(我使用的PhoneGap 2.7 )。
here is a part of my testing code in a java plugin (i'm using phonegap 2.7).
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
try {
String[] result;
result = new String[10];
result[0] = "test1 success";
result[1] = "test2 success";
callbackContext.success(result); //error
} catch(Exception ee) {
System.out.print("ee:"+ee.getMessage());
}
return true;
}
我的问题是:
-
如何返回从Android的PhoneGap的插件数组,所以我可以在JavaScript数组?
how to return the array from android phonegap plugin so i can get the array in the javascript?
该数据类型比数组好(JSONArray?)要返回?
which data type is better than array (JSONArray?) to be returned?
感谢
推荐答案
PluginResult 类是你的朋友:
public PluginResult(Status status, JSONObject message) {
this.status = status.ordinal();
this.message = (message != null) ? message.toString(): "null";
}
或
public PluginResult(Status status, String message) {
this.status = status.ordinal();
this.message = JSONObject.quote(message);
}
在你的情况下,它可以接受JSON对象或一个字符串。因此,要回报你需要一个JSON数组
In your case it accepts either a json object or a string. So to return a json array you need
JSONObject json = new JSONObject();
json.put("foo", "bar");
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, json));
这篇关于如何返回数组或其它集合元素从PhoneGap的Android插件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文