我可以将 Javascript 对象传递给 Android WebView 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9979398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I pass a Javascript object out to an Android WebView?
提问by Simon
I am migrating a web application into an android version. After having received and processed JSON data, I have an array of Javascript objects being held within the page.
我正在将 Web 应用程序迁移到 android 版本。接收并处理 JSON 数据后,我在页面中保存了一组 Javascript 对象。
How can I pass the complete contents of one of the javascript objects "out" to the webview container for display using native android controls ?
如何使用原生 android 控件将 javascript 对象之一的完整内容“输出”到 webview 容器以进行显示?
Eventually, I could create a javascript interface with a method having parameters for each of the possible javascript object properties - but this appears to be overly heavy.
最终,我可以创建一个 javascript 接口,其中的方法具有每个可能的 javascript 对象属性的参数 - 但这似乎过于繁重。
Can anyone help with this ?
有人能帮忙吗 ?
回答by Jason LeBrun
Android's WebView
contains a method called addJavascriptInterface(Object obj, String interfaceName)
that should be useful here.
Android'sWebView
包含一个addJavascriptInterface(Object obj, String interfaceName)
在这里应该有用的方法。
Using this method, the object obj
that you add as the interface can be accessed via JavaScript code in the Web View. In your case, you could pass in an object that has a setter method that transfers some JavaScript object back to Java.
使用此方法,obj
您添加为接口的对象可以通过 Web 视图中的 JavaScript 代码进行访问。在您的情况下,您可以传入一个具有 setter 方法的对象,该方法将一些 JavaScript 对象传输回 Java。
You'll still need to create the glue code that converts your JavaScript object into the JSON object. For a quick approach, you can just have your interface generate a JSONObject on the Java side using a JSON string passed from JavaScript. The JSONObject
class in Java has a constructor that accepts a String containing JSON data. So, you can pass the stringified result directly back to Java and create the object that way. For example:
您仍然需要创建将 JavaScript 对象转换为 JSON 对象的粘合代码。对于快速方法,您可以使用从 JavaScript 传递的 JSON 字符串让您的界面在 Java 端生成一个 JSONObject。JSONObject
Java 中的类有一个构造函数,它接受一个包含 JSON 数据的字符串。因此,您可以将字符串化的结果直接传递回 Java 并以这种方式创建对象。例如:
class JSInterface {
HashMap<String, JSONObject> mObjectsFromJS = new HashMap<String, JSONObject>();
public void passObject(String name, String json) {
mObjectsFromJS.put(name, new JSONObject(json));
}
}
//At some point, register using:
mJSInterface = new JSInterface();
mWebView.addJavascriptInterface(mJSInterface, "Android");
Then, on the JavaScript side, in the handler that has a blob of unparsed JSON in the variable jsonData:
然后,在 JavaScript 端,在变量 jsonData 中有一个未解析的 JSON 块的处理程序中:
Android.passObject("pageItems", jsonData);
Now, your JSInterface on the Java side will have a JSONObject containing the items, which you can access using the getters provided by JSONObject. The objects created via the Javascript call will be in the mObjectsFromJS
map. You'll of course want to add additional helper methods to the JSInterface class to allow for managing the objects better.
现在,Java 端的 JSInterface 将有一个包含项目的 JSONObject,您可以使用 JSONObject 提供的 getter 访问这些项目。通过 Javascript 调用创建的对象将在mObjectsFromJS
地图中。您当然希望向 JSInterface 类添加额外的辅助方法,以便更好地管理对象。
I haven't compiled or tested any of these methods, so you may have to tweak them a bit for proper operation. But hopefully this gives you the idea.
我还没有编译或测试过这些方法中的任何一个,因此您可能需要对它们进行一些调整才能正常运行。但希望这能给你这个想法。
However, if the objects have a consistent interface and data items, it would be more sensible to just create a simple JavaScript glue function that binds the JavaScript object properties to the Java-side object fields using setter methods.
但是,如果对象具有一致的接口和数据项,那么创建一个简单的 JavaScript 粘合函数会更明智,该函数使用 setter 方法将 JavaScript 对象属性绑定到 Java 端对象字段。
PLEASE NOTEThis is giving remote code ability to trigger native code on your device. If you do not have complete control over the pages/scripts being loaded into the WebView
, you should ensure that the behavior exposed by obj
doesn't allow any exploits.
请注意,这使远程代码能够在您的设备上触发本机代码。如果你没有在网页完全控制/脚本加载到WebView
,你应该确保被曝光的行为obj
不允许有任何漏洞。