Dann
Dann

Reputation: 23

Add additional args to cordova.exec function

I have a question, I am using a plugin in cordova, but I need to pass 2 args, I want to know if this structure can be inside the exec function

I need add "[str2]"

cordova.exec(callback, function(err) {
        callback('Nothing to echo.');
    }, "Echo", "echo", [str], [str2]);

Android : I need add JSONArray args2

 public boolean execute(String action, JSONArray args, JSONArray args2,CallbackContext callbackContext) throws JSONException {
    //..
        } catch (Exception e) {
          callbackContext.error("Error");
        }
        return true;
    }
    return false;
}

I hope you can help me or understand a little better!

Upvotes: 0

Views: 707

Answers (2)

Karthik
Karthik

Reputation: 1131

You can pass any number of args in the array as shown below.

 cordova.exec(callback, function(err) {
    callback('Nothing to echo.');
 }, "Echo", "echo", [arg1, arg2, arg3,....]);

While accessing the code in android you do it as

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    data1 = args.optString(0);
    data2 = args.optString(1);
    .
    .
    .
    } catch (Exception e) {
      callbackContext.error("Error");
    }
    return true;
}
return false;
}

Upvotes: 2

QuickFix
QuickFix

Reputation: 11721

args is an array, so if you need to have several parameters just put this parameters in the array instead of trying to add a second array containing only one element :

cordova.exec(callback, function(err) {
        callback('Nothing to echo.');
    }, "Echo", "echo", [str, str2]);

And then on the java side you get your strings by args.optString(0) and args.optString(1)

Upvotes: 1

Related Questions