How to pass JSON-formatted data from a WebView to a HTML page How to pass JSON-formatted data from a WebView to a HTML page json json

How to pass JSON-formatted data from a WebView to a HTML page


I copy pasted your code and it works (nothing is shown because you dont show the data) but the callback made from the Javascript into Android is executed correctly. You can check it with this code:

    WebView mWebView = (WebView) findViewById(R.id.webView1);    mWebView.getSettings().setJavaScriptEnabled(true);    mWebView.addJavascriptInterface(this, "webConnector");    mWebView.addJavascriptInterface(this, "toaster");    mWebView.loadUrl("file:///android_asset/table.html");    }    public String load() {        Log.e("HelloJavascript","HelloJavascript");        return "{\"key\":\"data\"}";    }    public void print(String message){        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();    }

And the HTML

    <html>    <head>    <title>Test</title>    <script type="text/javascript">    function loader() {        var jsonData = window.webConnector.load();        toaster.print(jsonData);    }    </script>    </head>    <body onload="loader()">    Do nothing    </body>    </html>