2

I'm tring to pass a multi-dimensional array using Javascript interface but it returns null when I get on the device.

Here is my javascript interface:

@JavascriptInterface
        public void showHTML(final String html) {
            this.html = html;
            processed = true;
            webView.loadUrl("javascript:" +
                    "var html = document.documentElement.innerHTML;" +
                    "var pre = html.indexOf('[{');" +
                    "var data = html.substring(pre, pre + html.substring(pre).indexOf(';'));" +
                    "var json = JSON.parse(data);" +
                    "var array  = [];" +
                    "var obj;" +
                    "for (i = 0; i<json.length; i++){" +
                        "obj = json[i];" +
                        "array.push([obj.StationName, obj.StationCode, obj.CountryName]);" +
                    "}" +
                    "window.Android.getData(array, json[0].StationName);");
        }

I'm tring to pass the variable array and the json[0].StationName is for test only.

Here is my getData():

  @JavascriptInterface
        public void getData(Object array, String test){
            Log.d("CPA", test);
            if(array == null){
                Log.d("CPA", "array is null");
            }
        }
1
  • Though I cannot find formal documentation, it seems that we cannot pass Javascript object to Java using addJavascriptInterface(). check the discussion over here: stackoverflow.com/questions/2250917/… Commented May 25, 2017 at 3:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.