ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-31 14:24:23  阅读:160  来源: 互联网

标签:android-webview html-parsing android-loader android


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有