我在 java 端有一个带有 webview 和 javascript 接口(interface)的 Activity。我想用 Java 编写一个可以从 webview 接受 json 参数的方法。
@JavascriptInterface
public String test(Object data) {
Log.d("TEST", "data = " + data);
}
在我调用的 webview javascript 上:
MyAPI.test({ a: 1, b: 2 });
但数据变量为空。
如何将 JSON 对象从 webview javascript 传递到本地方法?
谢谢
最佳答案
@njzk2 是对的,这样做:
在 JAVA 中:
@JavascriptInterface
public String test(String data) {
Log.d("TEST", "data = " + data);
return "this is just a test";
}
在 JS 中:
// some code
var result = test("{ a: 1, b: 2 }");
alert(result);
//some code
function test(args) {
if (typeof Android != "undefined"){ // check the bridge
if (Android.test!= "undefined") { // check the method
Android.test(args);
}
}
}
关于javascript - Android - 将 JSON 对象从 webview javascript 传递到 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22027161/