gpt4 book ai didi

Android:无法在 WebView 中加载具有 javascript 文件的简单 html 数据?

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:36 25 4
gpt4 key购买 nike

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 View 执行此操作时,它不会加载到我的 Web View 中。我已经启用了 javascript。我将 javascript 文件粘贴到 Assets 文件夹中。

最佳答案

首先你说的浏览器是你设备上的浏览器还是电脑上的浏览器?

现在,如果它在 android 浏览器(或您设备中的浏览器)中工作,那么问题是因为 javascript 文件位于您的 Assets 文件夹中。当您通过相对路径(您使用的方式)指示资源时, WebView 会相对于与您的 html 文件相同的文件夹搜索它。由于您使用字符串作为“html 文件”,我建议使用 loadDataWithBaseURL() .我在下面使用 assets 文件夹作为基本 URL 做了一个使用示例,试试吧。

web_view.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "UTF-8",null);

public void loadDataWithBaseURL (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'.

您可以尝试将 Assets 文件夹作为 baseUrl 传递,所以我猜您的代码应该是这样的

希望这对您有所帮助!

关于Android:无法在 WebView 中加载具有 javascript 文件的简单 html 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13834458/

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