1

I am trying to pass json data from a java to my web view this is my code:

interface:

public class ChartsJavaScriptInterface {
public ChartsJavaScriptInterface() {

}

    public String getData(){
        String s = "[ {date: 'A', frequency: .00100, date: '1-May-11', close: 1}," +
                    "{date: 'B', frequency: .01492, date: '2-May-14', close: 2}," +
                    "{date: 'C', frequency: .02782, date: '3-May-14', close: 3}," +
                    "{date: 'D', frequency: .04253, date: '4-May-15', close: 5}," +
                    "{date: 'D', frequency: .04253, date: '5-May-15', close: 4}," +
                    "{date: 'D', frequency: .04253, date: '7-May-15', close: 2},]";


        return s;
    }

}

activity:

    chartsView = (WebView) rootView.findViewById(R.id.charts_chart_view);

    chartsView.addJavascriptInterface(new ChartsJavaScriptInterface(), "chartInterface");
    chartsView.setWebChromeClient(new WebChromeClient());
    chartsView.getSettings().setJavaScriptEnabled(true);
    chartsView.setInitialScale(30);
    chartsView.getSettings().setBuiltInZoomControls(true);
    chartsView.getSettings().setLoadWithOverviewMode(true);
    chartsView.getSettings().setUseWideViewPort(true);
    chartsView.loadUrl("file:///android_asset/line.html");

html file:

....
draw(JSON.parse(chartInterface.getData()));
....

logCat: 08-25 13:34:57.397: E/Web Console(19809): Uncaught TypeError: Object [object Object] has no method 'getData' at file:///android_asset/line.html:345

what's my mistake? help please?

Thanks, Ilan

1 Answer 1

2

I believe you forgot to add the JavascriptInterface annotation:

@JavascriptInterface
public String getData(){
    String s = "[ {date: 'A', frequency: .00100, date: '1-May-11', close: 1}," +
                "{date: 'B', frequency: .01492, date: '2-May-14', close: 2}," +
                "{date: 'C', frequency: .02782, date: '3-May-14', close: 3}," +
                "{date: 'D', frequency: .04253, date: '4-May-15', close: 5}," +
                "{date: 'D', frequency: .04253, date: '5-May-15', close: 4}," +
                "{date: 'D', frequency: .04253, date: '7-May-15', close: 2},]";
    return s;
}

Please take a look here for a reference how to build a bridge between JS and Android

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but know i get Uncaught SyntaxError: Unexpected token d at file:///android_asset/line.html:1. and when i add the json hard coded as a var data =[...] it works. this error only happens when i use the interface
@ilan it's not the problem of Android now, it is a problem of JSON. You can start from checking is it valid or not. If you check in debugger you will find, that at least you are getting result from getData, it means interface is working fine.

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.