繁体   English   中英

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

[英]How to upgrade javascript to utilize phonegap 2.3 custom plugin?

我已经构建了一个名为TCPIPCommPlugin的自定义插件,它使用Phonegap / Cordova 1.6.1。 一切都很棒,回调/插件结果很好。 但是我需要将它升级到Cordova 2.3。 原因是我们现在开始使用Win8以及iOS / Android。

除此之外,我在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.xx并没有那么多。现在说完所有这些,我已经仔细阅读了网络试图找到转换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
                     }catch (JSONException jsonEx) {    
        System.out.println(jsonEx.toString());
        result = new PluginResult(Status.JSON_EXCEPTION);
    }
    return true;
}

这似乎与更新的代码相匹配。 我无法找到的是更新JAVASCRIPT调用以使此插件与更新的CORDOVA一起使用的方法。

任何帮助/点正确的方向将非常感谢!

我使用了以下文档但没有成功。 http://docs.phonegap.com/en/2.3.0/guide_plugin-development_index.md.html#Plugin%20Development%20Guide

https://github.com/apache/cordova-android/tree/master/framework/src/org/apache/cordova

UPDATE

谢谢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时,我得到一个Object#没有方法'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构造函数方法,所以:

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;
}

这应该让你升级。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM