Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

How can I pass the complete contents of one of the javascript objects "out" to the webview container for display using native android controls ?

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.

Can anyone help with this ?

like image 954
Simon Avatar asked Apr 02 '12 15:04

Simon


People also ask

Does WebView support JavaScript?

JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

Is WebView deprecated in Android?

This interface was deprecated in API level 12. This interface is now obsolete.

How do you communicate between WebView and native Android?

2.1 To receive data from webview ,we can create an interface, which will enable webview to connect the native layer and pass data. From native layer, create a class and replicate the following. While configuring web view, we need to set JavaScript interface as above JSBridge class.

What is alternative of WebView in Android?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.


1 Answers

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

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.

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

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

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.

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.

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.

like image 198
Jason LeBrun Avatar answered Oct 13 '22 01:10

Jason LeBrun