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");
            }
        }
dgzz
  • 2,929
  • 6
  • 34
  • 56
  • Though I cannot find formal documentation, it seems that we cannot pass Javascript object to Java using addJavascriptInterface(). check the discussion over here: https://stackoverflow.com/questions/2250917/passing-a-javascript-object-using-addjavascriptinterface-on-android – Roger Huang May 25 '17 at 03:13

0 Answers0