Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Phonegap:JS对Java方法的调用仅在1种情况下有效_Java_Javascript_Cordova_Phonegap Plugins - Fatal编程技术网

Phonegap:JS对Java方法的调用仅在1种情况下有效

Phonegap:JS对Java方法的调用仅在1种情况下有效,java,javascript,cordova,phonegap-plugins,Java,Javascript,Cordova,Phonegap Plugins,我正在一个Phonegap项目中工作,在这个项目中,我正在使用自定义插件扩展Phonegap的基本功能。我遇到的一个愚蠢的问题是让插件做出正确的反应。如果使用'echo'参数调用插件,插件应该回答返回其匹配的参数名称,对于'echo2'也是如此 奇怪的部分: echo('Echo String', function(echoValue) { alert(echoValue); }); sync('Sync String', function(echoValue) { a

我正在一个Phonegap项目中工作,在这个项目中,我正在使用自定义插件扩展Phonegap的基本功能。我遇到的一个愚蠢的问题是让插件做出正确的反应。如果使用'echo'参数调用插件,插件应该回答返回其匹配的参数名称,对于'echo2'也是如此

奇怪的部分:

echo('Echo String', function(echoValue) {
      alert(echoValue);
});

sync('Sync String', function(echoValue) {
      alert(echoValue);
});
public class Echo extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        switch(action) {

            case "echo":    String message = args.getString(0);
                            this.echo("Call on: Echo.echo()" + message, callbackContext);
                            return true;

            case "echo2":   String message = args.getString(0);
                            this.echo("Call on: Echo.echo2()" + message, callbackContext);
                            return true;
        }
        return false;
    }

    private void echo(String message, CallbackContext callbackContext) {
        if (message != null && message.length() > 0) { 
            callbackContext.success(message);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }
}
“echo”返回预期的答案(它执行成功回调),而“echo2”变量返回错误回调。真的没有想法了

JS定义:相同的函数(只有第四个参数不同)

JS调用这些函数:

echo('Echo String', function(echoValue) {
      alert(echoValue);
});

sync('Sync String', function(echoValue) {
      alert(echoValue);
});
public class Echo extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        switch(action) {

            case "echo":    String message = args.getString(0);
                            this.echo("Call on: Echo.echo()" + message, callbackContext);
                            return true;

            case "echo2":   String message = args.getString(0);
                            this.echo("Call on: Echo.echo2()" + message, callbackContext);
                            return true;
        }
        return false;
    }

    private void echo(String message, CallbackContext callbackContext) {
        if (message != null && message.length() > 0) { 
            callbackContext.success(message);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }
}
Java类:

echo('Echo String', function(echoValue) {
      alert(echoValue);
});

sync('Sync String', function(echoValue) {
      alert(echoValue);
});
public class Echo extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        switch(action) {

            case "echo":    String message = args.getString(0);
                            this.echo("Call on: Echo.echo()" + message, callbackContext);
                            return true;

            case "echo2":   String message = args.getString(0);
                            this.echo("Call on: Echo.echo2()" + message, callbackContext);
                            return true;
        }
        return false;
    }

    private void echo(String message, CallbackContext callbackContext) {
        if (message != null && message.length() > 0) { 
            callbackContext.success(message);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }
}

对于所有有类似问题的人,以下是一些关于为什么这不起作用的信息:

首先:代码运行良好-问题不在这里

哪里出了问题?

当我问这个问题时,Java类被命名为Echo,它在调用Class方法时起作用。尝试调用任何其他方法失败,因为

Phonegap构建服务不允许直接包含插件

但是在我的例子中,它仍然部分起作用,因为Java类Echo恰好是Phonegap Build为我提供的一个标准插件

Phonegap构建中包含的这个Echo插件碰巧有一个方法Echo,它导致了成功的回调

进一步阅读后:

echo('Echo String', function(echoValue) {
      alert(echoValue);
});

sync('Sync String', function(echoValue) {
      alert(echoValue);
});
public class Echo extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        switch(action) {

            case "echo":    String message = args.getString(0);
                            this.echo("Call on: Echo.echo()" + message, callbackContext);
                            return true;

            case "echo2":   String message = args.getString(0);
                            this.echo("Call on: Echo.echo2()" + message, callbackContext);
                            return true;
        }
        return false;
    }

    private void echo(String message, CallbackContext callbackContext) {
        if (message != null && message.length() > 0) { 
            callbackContext.success(message);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }
}
一个名为plugman的工具(也是由Adobe开发的)通过将创建的插件添加到phonegap项目来处理自定义插件的实现。。。我仍在测试和学习这一点,官方信息(也是我发现的唯一信息)可在此处获得:


首先,
echo
echo2
应该在交换机中使用双引号,而不是单引号(在Java文件中)。而且,我想您需要的不是
String.message
,而是
String message
。第三,这没有任何意义,因为唯一可以得到错误回调的情况是
message
为非null和非空,但这不能发生在您提供的代码段中,双引号和字符串消息更新。谢谢@ColinMorelli。类中的错误回调将在稍后发挥作用。。。以同样的结果重建。。。