Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova.exec function doesn't run the native function

I try to make a cordova plugin in IBM worklight.

Javascript:

HelloWorld = {     
  sayHello: function (success, fail, resultType) { 
      Cordova.exec( 
          success, 
          fail, 
          "HelloWorld", 
          "HelloWorld", 
           [resultType]
      );
   }
};

function callFunction() {
    HelloWorld.sayHello(basarili, basarisiz, "sinan");
}

Java:

package com.Cordova1;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;

import android.util.Log;
public class HelloWorld extends CordovaPlugin {
    public boolean execute(String arg0, JSONArray arg1, String arg2) {
        Log.d("HelloPlugin", "Hello, this is a native function called from PhoneGap/Cordova!"); 
        return true;
    }
}

When I call callFunction I see that fail function worked. Also, I can't see any HelloPlugin message in log window. What can I do ?

like image 261
gumust Avatar asked Mar 09 '13 23:03

gumust


2 Answers

module 09_3 ApacheCordovaPlugin in the samples is indeed using the deprecated Plugin class instead of CordovaPlugin. I have rewritten the HelloWorldPlugin class in module 09_3 to eliminate the deprecated Cordova Plugin API usage. The sample is working fine.

package com.AndroidApacheCordovaPlugin;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

public class HelloWorldPlugin extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray arguments,
            CallbackContext callbackContext) throws JSONException {

        if (action.equals("sayHello")) {
            String responseText = "Hello world";
            try {
                responseText += ", " + arguments.getString(0);
                callbackContext.success(responseText);
                return true;
            } catch (JSONException e) {
                callbackContext.error(e.getMessage());
            }
        } else {
            callbackContext.error("Invalid action: " + action);
            return false;
        }
        return false;
    }
}
like image 156
Arik S Avatar answered Oct 03 '22 04:10

Arik S


A couple of things, 1) did you add a line for your plugin into the config.xml file? and 2) you seem to be overriding the wrong method in CordovaPlugin. It should be:

public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
like image 21
Simon MacDonald Avatar answered Oct 03 '22 04:10

Simon MacDonald