使用Rhino讓java執(zhí)行javascript的方法實(shí)例
下載Rhino https://developer.mozilla.org/en-US/docs/Rhino
把js.jar拷貝到項(xiàng)目工程
實(shí)現(xiàn)從Java中執(zhí)行js中的函數(shù)、從js中調(diào)用Java中的方法,代碼:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text1 = (TextView) findViewById(android.R.id.text1);
TextView text2 = (TextView) findViewById(android.R.id.text2);
text1.setText(runScript(JAVA_CALL_JS_FUNCTION, "Test", new String[] {}));
text2.setText(runScript(JS_CALL_JAVA_FUNCTION, "Test", new String[] {}));
}
/** Java執(zhí)行js的方法 */
private static final String JAVA_CALL_JS_FUNCTION = "function Test(){ return '農(nóng)民伯伯 java call js Rhino'; }";
/** js調(diào)用Java中的方法 */
private static final String JS_CALL_JAVA_FUNCTION = //
"var ScriptAPI = java.lang.Class.forName(\"" + MainActivity.class.getName() + "\", true, javaLoader);" + //
"var methodRead = ScriptAPI.getMethod(\"jsCallJava\", [java.lang.String]);" + //
"function jsCallJava(url) {return methodRead.invoke(null, url);}" + //
"function Test(){ return jsCallJava(); }";
/**
* 執(zhí)行JS
*
* @param js js代碼
* @param functionName js方法名稱
* @param functionParams js方法參數(shù)
* @return
*/
public String runScript(String js, String functionName, Object[] functionParams) {
Context rhino = Context.enter();
rhino.setOptimizationLevel(-1);
try {
Scriptable scope = rhino.initStandardObjects();
ScriptableObject.putProperty(scope, "javaContext", Context.javaToJS(MainActivity.this, scope));
ScriptableObject.putProperty(scope, "javaLoader", Context.javaToJS(MainActivity.class.getClassLoader(), scope));
rhino.evaluateString(scope, js, "MainActivity", 1, null);
Function function = (Function) scope.get(functionName, scope);
Object result = function.call(rhino, scope, scope, functionParams);
if (result instanceof String) {
return (String) result;
} else if (result instanceof NativeJavaObject) {
return (String) ((NativeJavaObject) result).getDefaultValue(String.class);
} else if (result instanceof NativeObject) {
return (String) ((NativeObject) result).getDefaultValue(String.class);
}
return result.toString();//(String) function.call(rhino, scope, scope, functionParams);
} finally {
Context.exit();
}
}
public static String jsCallJava(String url) {
return "農(nóng)民伯伯 js call Java Rhino";
}
}
注意,混淆的時(shí)候js.jar可能混淆不過去,請(qǐng)參照文章4.1的方法。
相關(guān)文章
SpringBoot JPA實(shí)現(xiàn)查詢多值
這篇文章主要為大家詳細(xì)介紹了SpringBoot JPA實(shí)現(xiàn)查詢多值,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08SpringBoot配置文件中系統(tǒng)環(huán)境變量存在特殊字符的處理方式
這篇文章主要介紹了SpringBoot配置文件中系統(tǒng)環(huán)境變量存在特殊字符的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02mybatis-plus多表關(guān)聯(lián)查詢功能的實(shí)現(xiàn)
本文給大家介紹mybatis-plus多表關(guān)聯(lián)查詢功能的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-11-11spring boot idea maven依賴找不到問題處理方法
這篇文章主要介紹了spring boot idea 偶爾maven依賴找不到問題,這里總結(jié)了幾種處理方法,方便嘗試排查,對(duì)spring boot idea maven依賴找不到問題感興趣的朋友跟隨小編一起看看吧2023-08-08詳解如何在SpringBoot項(xiàng)目中使用全局異常處理
在完整的項(xiàng)目開發(fā)中,異常的出現(xiàn)幾乎是無法避免的;如果凡是有可能出現(xiàn)異常的地方,我們都手動(dòng)的使用try-catch將其捕獲的話,會(huì)使得代碼顯得十分臃腫并且后期不好維護(hù)。本文介紹了pringBoot項(xiàng)目中使用全局異常處理的方法,需要的可以參考一下2022-10-10java?web實(shí)現(xiàn)簡單登錄注冊(cè)功能全過程(eclipse,mysql)
前期我們學(xué)習(xí)了javaweb項(xiàng)目用JDBC連接數(shù)據(jù)庫,還有數(shù)據(jù)庫的建表功能,下面這篇文章主要給大家介紹了關(guān)于java?web實(shí)現(xiàn)簡單登錄注冊(cè)功能的相關(guān)資料,需要的朋友可以參考下2022-07-07