我可以通过一个Javascript对象出一个Android的WebView?可以通过、对象、WebView、Javascript

2023-09-04 09:15:01 作者:睡觉不如睡你

我移植的Web应用程序到Android版本。 在收到并处理JSON数据后,我有页面内正在举行Javascript对象的数组。

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.

使用原生的Andr​​oid控件我如何通过JavaScript对象走出去到的WebView容器的一个完整内容的显示?

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对象属性的参数的方法的JavaScript界面​​ - 而这似乎是过于沉重

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.

谁能帮助呢?

推荐答案

Android的的WebView 包含一个名为 addJavascriptInterface方法(obj对象,字符串接口名) ,应该会有帮助。

Android's WebView contains a method called addJavascriptInterface(Object obj, String interfaceName) that should be useful here.

使用此方法,对象 OBJ 所添加的接口可以通过JavaScript code。在Web视图进行访问。你的情况,你可以传递一个对象,具有传输一些JavaScript对象返回到Java的setter方法​​。

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.

您仍然需要创建胶水code,你的JavaScript对象转换成JSON对象。对于一个快速的方式,你可以有你的界面生成的JSONObject使用从JavaScript传递一个JSON字符串在Java端。该的JSONObject 中的Java类,接受包含JSON数据的字符串构造函数。所以,你可以通过字符串化的结果直接返回给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:

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");

然后,在JavaScript端,在具有未解析的JSON在可变jsonData一个blob的处理程序:

Then, on the JavaScript side, in the handler that has a blob of unparsed JSON in the variable jsonData:

Android.passObject("pageItems", jsonData);

现在,您JSInterface在Java方面将有包含的项目,你可以利用的JSONObject提供的干将访问的JSONObject。通过JavaScript调用创建的对象会在 mObjectsFromJS 地图。当然,你需要添加额外的辅助方法的JSInterface类,允许管理对象更好。

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.

我还没有编译或测试的任一方法,所以你可能需要调整他们一点正常运行。但希望这给你的想法。

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.

但是,如果对象有一个一致的界面和数据项,这将是更明智的只是创建一个简单的JavaScript粘贴功能结合JavaScript对象的属性使用setter方法​​的Java端对象字段。

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.

请注意 这是给触发本地code设备上的远程code的能力。如果你没有完全控制网页/脚本加载到的WebView ,你应该确保的行为暴露出来 OBJ 不允许任何攻击。

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.