Android:无法在WebView中加载具有javascript文件的简单html数据?
作者:互联网
WebView web_view = (WebView) findViewById(R.id.webView1);
web_view.getSettings().setJavaScriptEnabled(true);
web_view.getSettings().setPluginsEnabled(true);
web_view.getSettings().setAllowFileAccess(true);
String data;
data = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
+ "<html>"
+ "<head>"
+ "<title>My First chart using FusionCharts XT</title>"
+ "<script type=\"text/javascript\" src=\"FusionCharts.js\">"
+ "</script>"
+ "</head>"
+ "<body>"
+ "<div id=\"chartContainer\">FusionCharts XT will load here!</div>"
+ "<script type=\"text/javascript\">"
+ "FusionCharts.setCurrentRenderer(\"javascript\");"
+ "var myChart = new FusionCharts(\"FusionCharts/Line.swf\", \"myChartId\", \"400\", \"300\", \"0\", \"1\" );"
+ "var dataString =\"<chart> <set label='0.00' value='0'/><set label='5.00' value='2' /><set label='7.00' value='3' /><set label='9.00' value='4' /><set label='12.00' value='2' /></chart>\"; "
+ "myChart.setXMLData(dataString);"
+ "myChart.render(\"chartContainer\");" + "</script>"
+ "</body>" + "</html>";
Log.i("info", "Html " + data);
web_view.loadData(data, "text/html; charset=UTF-8",null);
在我的项目中,我正在使用融合图.我正在制作一个html字符串数据,并将其加载到WebView中,如上述代码示例中所示.当我在浏览器中运行此html文件时,它会运行并显示Fusion图表,但是当我使用android Web视图进行此操作时,它不会加载到我的Web视图中.
我已经启用了javascript.
我将javascript文件粘贴到资产文件夹中.
解决方法:
首先,您提到的浏览器是设备上的浏览器还是计算机中的浏览器?
现在,如果它可以在android浏览器(或设备中的浏览器)中工作,则问题是因为javascript文件位于您的资产文件夹中.当您通过相对路径(使用方式)指示资源时,Webview会相对于html文件所在的文件夹来搜索资源.由于您将字符串用作“ html文件”,因此我建议使用loadDataWithBaseURL().下面使用资产文件夹作为基本URL,创建了一个用法示例,请尝试一下.
web_view.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "UTF-8",null);
public void 07001 (String baseUrl, String data, String mimeType, String encoding, String historyUrl)
Added in API level 1
Loads the given data into this WebView, using baseUrl as the base URL for the content. The base URL is used both to resolve relative URLs and when applying JavaScript’s same origin policy. The historyUrl is used for the history entry.Note that content specified in this way can access local device files (via ‘file’ scheme URLs) only if baseUrl specifies a scheme other than ‘http’, ‘https’, ‘ftp’, ‘ftps’, ‘about’ or ‘javascript’.
If the base URL uses the data scheme, this method is equivalent to calling loadData() and the historyUrl is ignored.
Parameters
baseUrl the URL to use as the page’s base URL. If null defaults to ‘about:blank’.
data a String of data in the given encoding
mimeType the MIMEType of the data, e.g. ‘text/html’. If null, defaults to ‘text/html’.
encoding the encoding of the data
historyUrl the URL to use as the history entry. If null defaults to ‘about:blank’.
您可以尝试将资产文件夹作为baseUrl传递,所以我的猜测是您的代码将像这样
希望这可以帮助!
标签:android-webview,html-parsing,android-loader,android 来源: https://codeday.me/bug/20191031/1976389.html