5

I'm trying to pass an array from my activity to javascript method in html file located in Assets dir.

I'm using JavascriptInterface passing my int array like JSONArray:

public class JavaScriptInterface {

        Context mContext;       
        JavaScriptInterface(Context c) {

            mContext = c;

        }

        @JavascriptInterface
        public JSONArray getValues() {

            String values = "[100,133,'',120,122,132,133]";

            JSONArray jsonarr = null;

            try {

                jsonarr = new JSONArray(values);

            }

            catch(JSONException e) {

                e.printStackTrace();
            }

            return jsonarr;
        }


    }

In javascript method, I take the values thus:

    var data = JSON.parse(js.getValues());

Now, I'm testing my project on different devices and AVD:

the code works fine on Samsung Note 2 (JB 4.2.1) and on AVD with target Google APIs (API level 8) while on Asus Nexus 7 (JB 4.2.2) and others AVD with JB 4.2 the code stops working returns an Web Console error:

03-25 16:35:12.809: E/Web Console(11352): Uncaught SyntaxError: Unexpected token o at file:///android_asset/data/test.html:1

I need these values for represent a chart using a Javascript library.

In addition, I modified the file proguard-project.txt denying the javascript code obfuscation:

keepclassmembers class fqcn.of.javascript.interface.for.webview {
   public *;
}
-keep public class com.XXX.XXX.DataReportActivity$JavaScriptInterface
-keep public class * implements com.XXX.XXX.DataReportActivity$JavaScriptInterface
-keep classmembers class com.XXX.XXX.DataReportActivity$JavaScriptInterface {
    <fields>;
    <methods>;
}
-keepattributes JavascriptInterface

does anyone have any idea about solve it?

please, any help is welcome!

1 Answer 1

4

Change your Javascript Interface to return a String. JSON.parse is expecting a string, not a JSON object.

@JavascriptInterface
    public String getValues() {
        String values = "[100,133,'',120,122,132,133]";
        return values;
    }

The values string might need to be in quotes for the javascript function to be able to parse it too. In other words:

return "'" + values + "'";
Sign up to request clarification or add additional context in comments.

Comments

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.