javascript 如何从java代码调用javascript函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11739328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:11:17  来源:igfitidea点击:

How to call javascript function from java code

javajavascriptandroidcordova

提问by luperxe

I'm trying to call a javascript function from a java class, but I get these errors:

我正在尝试从 java 类调用 javascript 函数,但出现以下错误:

-Could not find method sun.misc.Service.installedProviders, referenced from method javax.script.ScriptEngineManager.initEngines

-Could not find method sun.misc.Service.providers, referenced from method javax.script.ScriptEngineManager.initEngines

-Could not find method sun.misc.Service.installedProviders, referenced from method javax.script.ScriptEngineManager.initEngines

-java.lang.VerifyError: javax.script.ScriptEngineManager

Here the code:

这里的代码:

    public  void sendResult(){
    ScriptEngineManager manager = new ScriptEngineManager();
                    ScriptEngine engine = manager.getEngineByName("js");

                    String script = "function send() {"+"var id_result = window.MyPGP.getResult();"+                            
                            "document.getElementById('id_result').value = id_result;"+"console.log(\"change the box value\");";

                    try {
                        engine.eval(script);

                        Invocable invocableEngine = (Invocable) engine;

                        invocableEngine.invokeFunction("send");


   } catch (ScriptException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }  catch (Exception e) {
                    e.printStackTrace();
                }

What I'm trying exactly to do is that when the payment is finished, this function is called in other methods in this class to get the result of the payment and it is printed in a box in the main html.

我真正想要做的是,当支付完成时,这个函数在这个类的其他方法中被调用以获取支付的结果,并将它打印在主 html 的一个框中。

回答by Himanshu

 ScriptEngine engine = manager.getEngineByName("JavaScript");

Does this work?

这行得通吗?

回答by Coder_sLaY

You can do something like this

你可以做这样的事情

super.loadUrl("file:///android_asset/www/index.html", 20000);
super.loadUrl("javascript: { var pageFlag = '" + flag + "';}"); // Your Javascript function here

回答by diegol

use this code

使用此代码

import javax.script.*;

public class InvokeScriptFunction {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");

        // JavaScript code in a String
        String script = "function hello(name) { print('Hello, ' + name); }";
        // evaluate script
        engine.eval(script);

        // javax.script.Invocable is an optional interface.
        // Check whether your script engine implements or not!
        // Note that the JavaScript engine implements Invocable interface.
        Invocable inv = (Invocable) engine;

        // invoke the global function named "hello"
        inv.invokeFunction("hello", "Scripting!!" );
    }
}

good luck

祝你好运