gpt4 book ai didi

java - 如何升级 javascript 以使用phonegap 2.3 自定义插件?

转载 作者:行者123 更新时间:2023-12-02 07:21:02 24 4
gpt4 key购买 nike

我构建了一个名为 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 执行(字符串操作,JSONArray数据,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 很好,我忘了在论坛代码中包含返回。哎呀。

现在我尝试像往常一样调用该函数,并且在第一次调用 Close/Open 时得到一个 Object # has no method '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;
}

这应该会让你升级。

关于java - 如何升级 javascript 以使用phonegap 2.3 自定义插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14238170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com