How to return an array or other types of collection elements from the phonegap android plugin

here is part of my test code in a java plugin ( I am 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; } 

My questions:

  • how to return an array from the phone phone plugin so i can get the array in javascript?

  • which data type is better than an array (JSONArray?) to return?

thanks

+4
source share
1 answer

The PluginResult class is your friend:

 public PluginResult(Status status, JSONObject message) { this.status = status.ordinal(); this.message = (message != null) ? message.toString(): "null"; } 

or

 public PluginResult(Status status, String message) { this.status = status.ordinal(); this.message = JSONObject.quote(message); } 

In your case, it takes 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)); 
+7
source

Source: https://habr.com/ru/post/1498116/


All Articles