2016-07-25 3 views
0

Java에서 매개 변수가있는 Android 함수를 호출하고 싶습니다. 호출하고 결과를 얻는 데 webview에서로드 할 필요가 없습니다. assets 폴더에있는 JS 파일webview가없는 매개 변수가있는 Android 호출 javascript 함수

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에서 오는 보낼 수 있습니다.

자바 스크립트 함수는이 개 매개 변수를 사용하고 어떤 도움에 감사드립니다 JSON

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

     return authenString; 
    } 

을 반환합니다. 여기

답변

1

내가 안드로이드에서 코뿔소를 사용하는 방법은 다음과 같습니다

/** 
    * 
    * @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에서이 대부분을 가지고 있지만, 지금 질문을 찾을 수 있다고 생각합니다.

관련 문제