简体   繁体   English

我可以将Javascript对象传递给Android WebView吗?

[英]Can I pass a Javascript object out to an Android WebView?

I am migrating a web application into an android version. 我正在将Web应用程序迁移到Android版本。 After having received and processed JSON data, I have an array of Javascript objects being held within the page. 在收到并处理了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 ? 如何将其中一个javascript对象的完整内容“out”传递给webview容器,以便使用本机android控件进行显示?

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 ? 有人能帮忙吗 ?

Android's WebView contains a method called addJavascriptInterface(Object obj, String interfaceName) that should be useful here. Android的WebView包含一个名为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. 使用此方法,可以通过Web视图中的JavaScript代码访问作为接口添加的对象obj In your case, you could pass in an object that has a setter method that transfers some JavaScript object back to Java. 在您的情况下,您可以传入一个具有setter方法的对象,该方法将一些JavaScript对象传回Java。

You'll still need to create the glue code that converts your JavaScript object into the JSON object. 您仍然需要创建将JavaScript对象转换为JSON对象的粘合代码。 For a quick approach, you can just have your interface generate a JSONObject on the Java side using a JSON string passed from JavaScript. 对于快速方法,您可以使用从JavaScript传递的JSON字符串让您的接口在Java端生成JSONObject。 The JSONObject class in Java has a constructor that accepts a String containing JSON data. Java中的JSONObject类有一个构造函数,它接受包含JSON数据的String。 So, you can pass the stringified result directly back to Java and create the object that way. 因此,您可以将字符串化结果直接传递回Java并以此方式创建对象。 For example: 例如:

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 blob的处理程序中:

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. 现在,Java端的JSInterface将包含一个包含项的JSONObject,您可以使用JSONObject提供的getter访问这些项。 The objects created via the Javascript call will be in the mObjectsFromJS map. 通过Javascript调用创建的对象将位于mObjectsFromJS映射中。 You'll of course want to add additional helper methods to the JSInterface class to allow for managing the objects better. 您当然希望向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 NOTE This 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公开的行为不允许任何漏洞利用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM