且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何返回数组或其它集合元素从PhoneGap的Andr​​oid插件类型

更新时间:2023-11-16 13:54:22

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));