简体   繁体   English

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

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

I have built a custom plugin called TCPIPCommPlugin that uses Phonegap/Cordova 1.6.1. 我已经构建了一个名为TCPIPCommPlugin的自定义插件,它使用Phonegap / Cordova 1.6.1。 Everything is all great and the callbacks/plugin results work just fine. 一切都很棒,回调/插件结果很好。 However I need to get it upgraded to Cordova 2.3. 但是我需要将它升级到Cordova 2.3。 The reason is that we are now starting dev on Win8 as well as iOS/Android. 原因是我们现在开始使用Win8以及iOS / Android。

That aside, I have the following code in javascript. 除此之外,我在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
};

This code continues with about 10-12 different function calls to the plugin, which is then concluded with... 此代码继续对插件进行大约10-12个不同的函数调用,然后以...结束

PhoneGap.addConstructor(function() {
    PhoneGap.addPlugin("TCPComm", new TCPComm());
});

Within the javascript itself, the actual function call looks like this. 在javascript本身中,实际的函数调用看起来像这样。

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

Additionally here is what the JAVA Plugin looks like. 另外这里是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;}

This works great with Cordova 1.6.1. 这适用于Cordova 1.6.1。 However not so much with Cordova 2.xx Now with all this said and done, I have perused the web trying to find a way to convert the JAVA. 然而,与Cordova 2.xx并没有那么多。现在说完所有这些,我已经仔细阅读了网络试图找到转换JAVA的方法。 I came up with the following. 我想出了以下内容。

public boolean execute(String action, JSONArray data, CallbackContext callbackContext) { 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;
}

This seems to match the updated code. 这似乎与更新的代码相匹配。 What I have not been able to find is a way to update the JAVASCRIPT calls to make this plugin work with the updated CORDOVA. 我无法找到的是更新JAVASCRIPT调用以使此插件与更新的CORDOVA一起使用的方法。

Any help/points in the right direction would be much appreciated! 任何帮助/点正确的方向将非常感谢!

I have used the following documentation with no success. 我使用了以下文档但没有成功。 http://docs.phonegap.com/en/2.3.0/guide_plugin-development_index.md.html#Plugin%20Development%20Guide 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 https://github.com/apache/cordova-android/tree/master/framework/src/org/apache/cordova

UPDATE UPDATE

Thanks for the response Simon. 谢谢Simon的回应。 In the interim I had replaced my javascript with the following. 在过渡期间,我用以下内容替换了我的javascript。 Do I need to revert it back to what it was prior? 我是否需要将其恢复到以前的状态?

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

UPDATE #2 - Fixed some errors 更新#2 - 修正了一些错误

I went back to the old javascript and replaced it all so it looks like this now.. 我回到旧的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]);
};

I also replaced the constructor with.. 我也用...替换了构造函数

    if(!window.plugins) {
    window.plugins = {};
}
if (!window.plugins.TCPComm) {
    window.plugins.TCPComm = new TCPComm();
}

Now when I run it in Chrome, (UI debugging), I can see the plugin built with all the proper functions inside the function. 现在当我在Chrome中运行它(UI调试)时,我可以看到该插件内置了函数内所有正确的函数。

The Java was fine, I forgot to include the return on the forum code. Java很好,我忘了在论坛代码中包含返回。 Whoops. 哎呦。

Now I tried calling the function like I always have and I get a Object # has no method 'exec' at the first JAVASCRIPT call of either Close/Open.. 现在我尝试调用函数,就像我一直有的一样,在第一次JAVASCRIPT调用Close / Open时,我得到一个Object#没有方法'exec'。

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

I have breakpoints set in the Java on the execute to let me know when the plugin has called successfully to the Java and still no luck. 我在执行的Java中设置了断点,让我知道插件成功调用Java时仍然没有运气。

Any other thoughts? 还有其他想法吗? Thanks again for your insight. 再次感谢您的见解。

Replace PhoneGap.exec with the following code: 用以下代码替换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
};

The add constructor method is deprecated so do: 不推荐使用add构造函数方法,所以:

if(!window.plugins) {
    window.plugins = {};
}
if (!window.plugins.videoPlayer) {
    window.plugins.TCPComm = new TCPComm();
}

For your Java code you are most of the way there but you need to return the result: 对于您的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;
}

That should get you upgraded. 这应该让你升级。

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

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