Cordova.exec function doesn't run the native function
Asked Answered
T

4

8

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 ?

Toil answered 9/3, 2013 at 23:8 Comment(0)
C
5

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;
    }
}
Cankered answered 10/3, 2013 at 19:29 Comment(0)
L
2

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)
Ludwig answered 10/3, 2013 at 5:4 Comment(1)
Yes I added a line into config.xml. <plugin name="HelloWorld" value="com.Cordova1.HelloWorld"/> I tried the second one you said, but I see same things again.Toil
L
0

I was having the same problem. Have a look at module 09_3 ApacheCordovaPlugin in the samples. That example does work for me, but they are using the deprecated Plugin class instead of CordovaPlugin.

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;

...

public class HelloWorldPlugin extends Plugin {

    public PluginResult execute(String action, JSONArray arguments, String callbackId) {

The deprecated class returns PluginResult, not a boolean. I've tried the same code using the CordovaPlugin signature and that results in a fail every time. Apparently whatever WL code is invoking the plugin is apparently expecting the signature of the deprecated class.

Lubet answered 10/3, 2013 at 16:9 Comment(2)
I looked at the 09_3 ApacheCordovaPlugin, I tried this and I tried again now, but the project still results in a fail every time. Have you got an example that works for this in your hand ? Thank you.Toil
Sorry but 09_3 ApacheCordovaPlugin does work for me, and the rewritten version above that extends CordovaPlugin also works. They work in the emulator and directly on a device. You aren't trying to run it in the browser simulator are you? That will not work.Lubet
T
0

I solved the problem. I use the version 2.4 of cordova. I can't understand why it didn't work. when I use "cordova.exec" it doesn't work, however when I use PhoneGap.exec it works.

Also I looked for the definition; In the last line of cordova-2.4.0.js, it says var PhoneGap = cordova; Ok, Phonegap was defined, but I don't know why cordova doesn't work.

Thank you for your answers.

Toil answered 13/3, 2013 at 23:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.