【问题标题】:How to upgrade javascript to utilize phonegap 2.3 custom plugin?如何升级 javascript 以利用 phonegap 2.3 自定义插件?
【发布时间】:2013-01-09 15:03:48
【问题描述】:

我已经构建了一个名为 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.x.x 并没有那么多。现在说了这么多,我已经仔细阅读了网络,试图找到一种转换 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

更新

感谢西蒙的回复。在此期间,我用以下内容替换了我的 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 调用中,我得到一个 Object # has no method 'exec'。

    window.plugins.TCPComm.Close("", Dummy, Dummy);
window.plugins.TCPComm.Open(g_IpAddr, OpenOK,OpenFail);

我在执行时在 Java 中设置了断点,让我知道插件何时成功调用 Java,但仍然没有运气。

还有其他想法吗?再次感谢您的洞察力。

【问题讨论】:

    标签: java javascript android cordova


    【解决方案1】:

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

    这应该会让你升级。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      相关资源
      最近更新 更多