2016-07-25 76 views
0

我想在android应用程序中调用带有Java参数的javascript函数,我不需要在webview中调用它,因为我只需要调用它并从中获取结果在资产文件夹中的JS文件。Android调用javascript函数,不带webview参数

我在iOS上使用JavascriptCore做了它,但我无法在android中找到相同的功能。

抬头看看AndroidJSCore和Rihno,但文档和教程目前还不清楚。

我加载JS文件到一个字符串,进一步我不能去如何发送参数和获得结果。

下面是如何将文件加载到一个字符串:

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; 
    } 

任何帮助理解。

回答

1

以下是我在Android上使用犀牛:

/** 
    * 
    * @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; 
    } 

我觉得我得到了最本从左右,但现在找不到的问题。