我正在制作我的第一个phonegap构建插件,但我无法将数据从java返回到javascript。
此时我已完成了我认为需要做的所有设置工作,但能够将任何内容返回给回调。
java包含了我认为必需的内容
package com.power_6.baseball_native_card_calculation;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class CardCalculation extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("getCardBatter")) {
callbackContext.success("it works");
} else {
callbackContext.error("it failed");
return false;
}
return true;
}
}
我的Javascript或者是:
if (window.cordova) {
var exec = require("cordova/exec");
var getCardCalculation = {
getCardBatter: function (successCallback, failureCallback) {
exec(
successCallback,
failureCallback,
"CardCalculation", "getCardBatter", []);
console.log('batter ran')
},
getCardPitcher: function (successCallback, failureCallback) {
exec(
successCallback,
failureCallback,
"CardCalculation", "getCardPitcher", []);
console.log('pitcher ran');
}
}
module.exports = getCardCalculation;
function cardCalculateSuccessCall (){
console.log('Works');
//console.log(response);
}
function cardCalculateErrorCall (){
console.log('Failed');
//console.log(response);
}
}
每当我从PhoneGap Build Debug窗口内部调用它时,我都没有从回调中得到任何响应。
我不确定还包括哪些内容,但感谢大家提供的任何帮助。
GitHub就是:https://github.com/cdearcangelis/baseball-native-card-calculation Phonegap Build Plugin是:https://build.phonegap.com/plugins/2327
答案 0 :(得分:0)
这应该有效:
getCardCalculation.getCardBatter(function(result){alert(result);},function(error){alert(error);});