如何升级javascript以利用phonegap 2.3自定义插件?

如何升级javascript以利用phonegap 2.3自定义插件?,java,javascript,android,cordova,Java,Javascript,Android,Cordova,我构建了一个名为tcpipcomplugin的定制插件,它使用Phonegap/Cordova 1.6.1。一切都很好,回调/插件结果也很好。但是我需要升级到Cordova 2.3。原因是我们现在正在Win8和iOS/Android上启动dev 除此之外,我还有以下javascript代码 var TCPComm = function() { }; TCPComm.prototype.Open = function(Cmd,successCallback, failureCallback) {

我构建了一个名为tcpipcomplugin的定制插件,它使用Phonegap/Cordova 1.6.1。一切都很好,回调/插件结果也很好。但是我需要升级到Cordova 2.3。原因是我们现在正在Win8和iOS/Android上启动dev

除此之外,我还有以下javascript代码

var TCPComm = function() {
};

TCPComm.prototype.Open = function(Cmd,successCallback, failureCallback) {
 return PhoneGap.exec(    successCallback,    //Success callback from the plugin
                          failureCallback,     //Error callback from the plugin
                          'TCPIPCommPlugin',  //Tell PhoneGap to run "DirectoryListingPlugin" Plugin
                          'Open',              //Tell plugin, which action we want to perform
                          [Cmd]);        //Passing list of args to the plugin
};
这段代码将继续对插件进行大约10-12个不同的函数调用,然后以

PhoneGap.addConstructor(function() {
    PhoneGap.addPlugin("TCPComm", new TCPComm());
});
在javascript本身中,实际的函数调用如下所示

window.plugins.TCPComm.Open(g_IpAddr, OpenOK,OpenFail);
此外,下面是JAVA插件的外观

@Override
    public PluginResult execute(String action, JSONArray data, String callbackId) {
           PluginResult result = null;
        try {
            Actions currentAction = Actions.valueOf(action.toUpperCase());
            JSONObject Resp = new JSONObject();
            String RespStr;
            switch(currentAction){
            case OPEN:
            {
                       //do work
                    }catch (JSONException jsonEx) { 
        System.out.println(jsonEx.toString());
        result = new PluginResult(Status.JSON_EXCEPTION);
    }
    return result;}
这对Cordova 1.6.1非常有效。然而,Cordova 2.x.x的情况并非如此。现在说了这么多,做了这么多,我仔细阅读了web,试图找到一种转换JAVA的方法。我得出了以下结论

公共布尔执行(字符串操作、JSONArray数据、CallbackContext CallbackContext){

这似乎与更新后的代码相匹配。我没有找到一种方法来更新JAVASCRIPT调用,使这个插件与更新后的CORDOVA一起工作

如有任何方向正确的帮助/建议,将不胜感激

我使用了以下文档,但没有成功。

更新

感谢Simon的回复。在此期间,我用以下内容替换了我的javascript。我需要将其恢复到之前的状态吗

cordova.define("cordova/plugin/TCPIPCommPlugin", function(require, exports, module){
    var exec = require('cordova/exec');
    var TCPComm = function() {};
//  var TCPCommError = function(code, message) {
//        this.code = code || null;
//        this.message = message || '';
//    };

    TCPComm.prototype.Open = function(success,fail) {
          return cordova.exec(              successCallback,    //Success callback from the plugin
                                  failureCallback,     //Error callback from the plugin
                                  'TCPIPCommPlugin',  //Tell PhoneGap to run "DirectoryListingPlugin" Plugin
                                  'Open',              //Tell plugin, which action we want to perform
                                  [Cmd]);
    };
    var TCPIPCommPlugin = new TCPComm();
    module.exports = TCPIPCommPlugin;
});
更新#2-修复了一些错误

我回到了旧的javascript并将其全部替换,所以现在看起来是这样的

var TCPComm = function() {}; 


TCPComm.prototype.Open = function(Cmd, successCallback,failureCallback) {
      return cordova.exec(              successCallback,    //Success callback from the plugin
                              failureCallback,     //Error callback from the plugin
                              'TCPIPCommPlugin',  //Tell PhoneGap to run "DirectoryListingPlugin" Plugin
                              'Open',              //Tell plugin, which action we want to perform
                              [Cmd]);
};
我还将构造函数替换为

    if(!window.plugins) {
    window.plugins = {};
}
if (!window.plugins.TCPComm) {
    window.plugins.TCPComm = new TCPComm();
}
现在,当我在Chrome(UI调试)中运行它时,我可以看到插件内置了函数中所有正确的函数

Java很好,我忘了在论坛代码中包含返回值。哎呀

现在我像往常一样尝试调用函数,得到一个对象#在第一次JAVASCRIPT调用Close/Open时没有方法'exec'

    window.plugins.TCPComm.Close("", Dummy, Dummy);
window.plugins.TCPComm.Open(g_IpAddr, OpenOK,OpenFail);
我在执行时在Java中设置了断点,让我知道插件何时成功调用Java,但仍然没有成功


还有其他想法吗?再次感谢您的见解。

用以下代码替换PhoneGap.exec:

var TCPComm = function() {
}; 

TCPComm.prototype.Open = function(Cmd,successCallback, failureCallback) {
 return cordova.exec(    successCallback,    //Success callback from the plugin
                      failureCallback,     //Error callback from the plugin
                      'TCPIPCommPlugin',  //Tell PhoneGap to run "DirectoryListingPlugin" Plugin
                      'Open',              //Tell plugin, which action we want to perform
                      [Cmd]);        //Passing list of args to the plugin
};
不推荐使用add constructor方法,请执行以下操作:

if(!window.plugins) {
    window.plugins = {};
}
if (!window.plugins.videoPlayer) {
    window.plugins.TCPComm = new TCPComm();
}
对于Java代码,您已经完成了大部分工作,但需要返回结果:

public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {

    PluginResult result = null;
    try {
        Actions currentAction = Actions.valueOf(action.toUpperCase());
        JSONObject Resp = new JSONObject();
        String RespStr;
        switch(currentAction){
        case OPEN:
            //do work
            this.callbackContext.sendPluginResult(
                new PluginResult(PluginResult.Status.OK, results));
    } catch (JSONException jsonEx) {    
        System.out.println(jsonEx.toString());
        result = new PluginResult(Status.JSON_EXCEPTION);
    }
    return true;
}
那会让你升级的

public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {

    PluginResult result = null;
    try {
        Actions currentAction = Actions.valueOf(action.toUpperCase());
        JSONObject Resp = new JSONObject();
        String RespStr;
        switch(currentAction){
        case OPEN:
            //do work
            this.callbackContext.sendPluginResult(
                new PluginResult(PluginResult.Status.OK, results));
    } catch (JSONException jsonEx) {    
        System.out.println(jsonEx.toString());
        result = new PluginResult(Status.JSON_EXCEPTION);
    }
    return true;
}