gpt4 book ai didi

javascript - 找不到 webview javascript 接口(interface)函数

转载 作者:行者123 更新时间:2023-11-29 21:02:49 27 4
gpt4 key购买 nike

我正在尝试将 json 数据从 java 传递到我的 WebView 这是我的代码:

界面:

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文件:

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

日志猫:08-25 13:34:57.397:E/Web 控制台(19809):未捕获的类型错误:对象 [object Object] 在 file:///android_asset/line.html:345 没有方法 'getData'

我的错误是什么?请帮忙?

谢谢,宜兰

最佳答案

我相信你忘了添加 JavascriptInterface 注释:

@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;
}

请看here引用如何在 JSAndroid

之间搭建桥梁

关于javascript - 找不到 webview javascript 接口(interface)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25484262/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com