2016-07-25 6 views
0

Javaのパラメータを使用してAndroidアプリケーションでjavascript関数を呼びたいのですが、呼び出すだけで結果を得る必要があるため、webviewで読み込む必要はありませんアセットフォルダにあるJSファイル。AndroidのJavaScript関数をwebviewなしのパラメータで呼び出す

私はJavascriptCoreを使ってiOS上でやったが、アンドロイドで同じ機能を見つけることはできない。

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

を返します。ここで

答えて

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

私はSOからこれのほとんどを得たと思いますが、今、質問を見つけることができません。

関連する問題