繁体   English   中英

带有参数的Android调用javascript函数,没有webview

[英]Android call javascript function with parameters without webview

我想在Android应用程序中使用Java中的参数调用javascript函数,而无需在webview中加载它,因为我只需要调用它并从资产文件夹中的JS文件获取结果即可。

我是使用JavascriptCore在iOS上完成的,但是在android中找不到相同的功能。

查找了AndroidJSCore和Rihno,但是关于该主题的文档和教程不清楚。

我将JS文件加载到String中,进一步,我不知道如何发送参数和获取结果。

这是将文件加载到字符串中的方法:

    AssetManager assetManager = getAssets();
    String jsFile;

    // To load js file
    InputStream input;
    try {
        input = assetManager.open("authenticate.js");

        int size = input.available();
        byte[] buffer = new byte[size];
        input.read(buffer);
        input.close();

        // byte buffer into a string
        jsFile = new String(buffer);
        resultTV.setText(jsFile);
        Log.d("TAG", jsFile);
    } catch (IOException e) {
        e.printStackTrace();
    }

要发送的参数来自Edittexts。

javascript函数采用2个参数并返回JSON

   function authenticate(uName, pWord)
    {
        var authenString = JSON.stringify(authenJSON);

        return authenString;
    }

任何帮助表示赞赏。

这是我在Android中使用Rhino的方式:

/**
     *
     * @param javaScriptCode
     * @param functionNameInJavaScriptCode
     * @param params Do not pass an array of primitives!  For example if passing doubles, pass Double[], not double[]
     * @return
     */
    public Map<String,Object> execute(String javaScriptCode, String functionNameInJavaScriptCode, Iterable<String> returnObjectKeys, Object... params){

        Map<String,Object> rtn = null;
        // Every Rhino VM begins with the enter()
        // This Context is not Android's Context
        Context rhino = Context.enter();

        // Turn off optimization to make Rhino Android compatible
        rhino.setOptimizationLevel(-1);
        try {
            final Object[] parameters = new Object[params.length + 1];
            for(int i=0; i < params.length; i++){
                parameters[i] = params[i];
            }
            parameters[parameters.length - 1] = BuildConfig.DEBUG;

            Scriptable scope = rhino.initStandardObjects();

            rhino.evaluateString(scope, javaScriptCode, "JavaScript", 1, null);

            // Get the functionName defined in JavaScriptCode
            Object obj = scope.get(functionNameInJavaScriptCode, scope);

            if (obj instanceof Function) {
                Function jsFunction = (Function) obj;

                // Call the function with params
                Object jsResult = jsFunction.call(rhino, scope, scope, parameters);
                if(jsResult == null){
                    return null;
                }
                Scriptable s = (Scriptable) jsResult;
                rtn = convert(s, returnObjectKeys);
            }
            else {
                throw new IllegalArgumentException("No function " + functionNameInJavaScriptCode + " found in supplied script");
            }
        } finally {
            Context.exit();
        }

        return rtn;
    }

    private Map<String,Object> convert(Scriptable object, Iterable<String> keys){

        Map<String,Object> rtn = new HashMap<>();
        for(String s : keys){
            if(object.has(s,object)){
                rtn.put(s, object.get(s, object));
            }
        }

        return rtn;
    }

我想我从SO中得到了大部分,但现在找不到问题了。

暂无
暂无

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

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