0

I have an html page stored locally on my Android app assets. I want to display it within a WebView and to add some parameters to the web page.

I put all my values into a JSONArray and then I use 'addJavascriptInterface' to add this JSONArray to the webpage javascript.

But when I try to use the value in javascript, it's of type [object Object] and with a length undefined...

JSONArray json = buildJSONArray();

webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);

webView.addJavascriptInterface(json, "data");
webView.loadUrl("file:///android_asset/test.html");

I trie to replace the jsonArray by a simple String, and the same thing happen...

How can I send a javascript parameter to my html page from Android ?' Apparently I've miss something with the 'addJavascriptInterface' method...

Thanks :)

Ynnad
  • 289
  • 3
  • 18
  • Hi, please read http://stackoverflow.com/questions/4325639/android-calling-javascript-functions-in-webview – mguimard Aug 05 '16 at 14:02

1 Answers1

1

Android: return Json as string

JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "gokul");
json=jsonObject.toString();
return json;

javascript: parse the json string

var json=JSON.parse(json);
Gokul
  • 841
  • 7
  • 13