简体   繁体   English

Phonegap Cordova 2.2插件开发,无法在Java和JavaScript之间进行通信

[英]Phonegap Cordova 2.2 plugin development, cannot communicate between Java and JavaScript

I have been trying to get this to work for past few hours now. 我一直在尝试使它在过去的几个小时内正常工作。 I have cordova 2.2. 我有科尔多瓦2.2。

I created a new package called com.tester.newvideouri . 我创建了一个名为com.tester.newvideouri的新包。

I have a class called newVideoUri in this package with the following content 我在此包中有一个名为newVideoUri的类,其中包含以下内容

package com.tester.newvideouri;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * This class echoes a string called from JavaScript.
 */
public class newVideoUri extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("echo")) {
            String message = args.getString(0); 
            this.echo(message, callbackContext);
            return true;
        }
        return false;
    }

    private void echo(String message, CallbackContext callbackContext) {
        System.out.println("success here and display it");
        if (message != null && message.length() > 0) { 
            callbackContext.success(message);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }
}

In my config.xml I added following line: 在我的config.xml ,添加了以下行:

    <plugin name="Echo" value="com.tester.newvideouri.newVideoUri" />

in my javascript file I have following: 在我的javascript文件中,我有以下内容:

    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }
    window.onDeviceReady = function() {         
        window.echo = function(str, callback) {
            alert('Started');
            cordova.exec(callback, function(err) {
                callback('Nothing to echo.');
            }, "Echo", "echo", [ str ]);
            alert('The END');
        };          
        window.echo("echome", function(echoValue) {
            alert(echoValue == "echome"); // should alert true.
        });
    }

Nothing happens when I run the code. 当我运行代码时,什么都没有发生。 Can anyone please tell me what am I doing wrong? 谁能告诉我我在做什么错?

write your "echome" code into a function like this: 将您的“ echome”代码写入如下函数:

function echome(){
    window.echo("echome", function(echoValue){
    alert(echoValue == "echome");
    });
}

and call this function in html code like this: 并在html代码中调用此函数,如下所示:

<a href="javascript:echome();">Click here</a>

try to run it. 尝试运行它。 It was working for me. 它为我工作。 :) :)

Code is correct per this example: http://docs.phonegap.com/en/2.6.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android 此示例中的代码是正确的: http : //docs.phonegap.com/en/2.6.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android

Likely your issue is that cordova is failing to "hook" up. 您的问题可能是Cordova无法“挂断”。 You did added cordova.android.js to your web page right? 您确实将cordova.android.js添加到了网页上,对吗?

If so make sure the device ready is actually being called: 如果是这样,请确保已实际调用就绪设备:

window.onDeviceReady = function() {
  alert('ready to work'); 
   // if you do not see this alert nothing in cordova will work as 
   // it means communication was not started

}

You need to enter this in your javascript file: 您需要在JavaScript文件中输入以下内容:

cordova.define("cordova/plugin/sampleplugin", function(require, exports, module) {
  var exec = require('cordova/exec');
  var SamplePlugin = function() {};

  SamplePlugin.prototype.authenticate = function(loginId,plainTextPassword,successCallback,failureCallback) {
        return exec(successCallback, failureCallback, 'SamplePlugin', 'authenticate', [loginId,plainTextPassword]);

  }
        var sampleplugin = new SamplePlugin();
        module.exports = sampleplugin;
});

Here SamplePlugin is the name of my plugin. 这里SamplePlugin是我的插件的名称。

Also in your html file, write this code: 同样在您的html文件中,编写以下代码:

var sampleplugin = cordova.require("cordova/plugin/sampleplugin");
           function  callrestfulweb()
{
               var loginId= document.getElementById("loginId").value;
               var plainTextPassword = document.getElementById("plainTextPassword").value;
               sampleplugin.authenticate(loginId,plainTextPassword,function(){alert("login successful");},function(){alert("Invalid username or password ");})
       }

I hope you can understand through this example. 希望您能通过这个例子理解。

you need to enter this in your javascript file: 您需要在JavaScript文件中输入以下内容:

cordova.define("cordova/plugin/sampleplugin", function(require, exports, module) {
  var exec = require('cordova/exec');
  var SamplePlugin = function() {};

  SamplePlugin.prototype.authenticate = function(loginId,plainTextPassword,successCallback,failureCallback) {
        return exec(successCallback, failureCallback, 'SamplePlugin', 'authenticate', [loginId,plainTextPassword]);

  }
        var sampleplugin = new SamplePlugin();
        module.exports = sampleplugin;
});

Here SamplePlugin is the name of my 这里SamplePlugin是我的名字

Also in your html file,write this code 同样在您的html文件中,编写此代码

var sampleplugin = cordova.require("cordova/plugin/sampleplugin");
           function  callrestfulweb()
{
               var loginId= document.getElementById("loginId").value;
               var plainTextPassword = document.getElementById("plainTextPassword").value;
               sampleplugin.authenticate(loginId,plainTextPassword,function(){alert("login successful");},function(){alert("Invalid username or password ");})
       }

I hope you can understand through this example. 希望您能通过这个例子理解。

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

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