繁体   English   中英

Android Webview HTML加载Javascript问题

[英]Android Webview HTML Loading Javascript Problem

我一直在与这个问题争斗超过48小时,我无法在网上任何地方找到答案。 这是设置:

我的Android应用程序在首次运行时下载内容(内容超过20MB),文件解压缩到用户的SD卡/ mnt / sdcard / {my package} /文件夹中。 内容包括HTML文件,CSS文件,JS文件和图像。 这是写入SD卡的完整结构(其中/ = / mnt / sdcard / {my package} / folder /):

/内容/

a.html
b.html
images/
  image1.jpg

/ CSS /

c.css
d.css

/ JS /

e.js
f.js

这是我从SD卡加载html文件的代码:

    webView = (WebView) findViewById(R.id.pageBrowser);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new LinkHandlerInterface(), "Android");
    webView.setWebViewClient(new PageWebViewClient());

    // contentLocation + url is equal to the full path to the html file
    webView.loadUrl("file://" + contentLocation + url);

此代码成功加载HTML页面。 没有问题。 每个页面都有以下标题:

    <link rel="stylesheet" type="text/css" href="../css/screen.css" media="all" />
    <link rel="stylesheet" type="text/css" href="../css/inPractice.css" media="all" />
    <link rel="stylesheet" type="text/css" href="../css/inPracticeScheme.css" media="all" />
    <link rel="stylesheet" type="text/css" href="../css/mobile/iPad.css" media="all" />
    <script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="../js/inPractice-utilities.js"></script>
    <script type="text/javascript" src="../js/inPractice.js"></script>
    <script type="text/javascript" src="../js/mobile/inpractice.ipad.js"></script>

这就是问题所在。 我的WebView渲染HTML就好了。 它甚至可以完美地加载和应用CSS。 但是,它拒绝加载和执行Javascript。 如果你从上面记得,js文件夹实际上是从html文件中提取的,所以它指向正确的位置。

这是我所知道的清单:

  • 我正在使用的CSS正在应用,所以我知道问题不在于文件位置。

  • 我之前使用过相同的代码,但是从我的应用程序的资源文件夹(文件:/// android_assets / ...)加载文件,它运行得很好。 由于内容太大,我无法将其与我的应用程序捆绑在一起,因此转移到SD卡。

  • 如果我更改HTML文件的方式是所有Javascript方法都列在脚本标记内,它可以正常工作。 我无法控制HTML,因此我无法永久应用此更改。 这告诉我WebView执行Javascript没有问题。

  • 图像加载正常。

我现在的想法很新鲜。 有没有人知道为什么我的WebView无法加载我的Javascript文件? 谁看过这个吗?

编辑:这里是我试图使用的JS文件可以在这里查看:

http://www.automatastudios.com/clients/cco/inpractice/css/inPractice.css http://www.automatastudios.com/clients/cco/inpractice/css/inPracticeScheme.css http://www.automatastudios。 com / clients / cco / inpractice / css / screen.css http://www.automatastudios.com/clients/cco/inpractice/css/mobile/iPad.css

  • 注意:这些CSS文件不是由我创作的。

从来没有能够找到解决方案。

我最后只是编辑HTML文件,这样就将Javascript源文件更改为内联Javascript。 这有效(正如我预期的那样)。

加载引用js,css的html的首选方法是使用

webView.loadDataWithBaseURL()

我们需要将在文件系统上找到html,js,css的位置的根目录作为baseURL传递。 这里要注意的重要一点是我们只需要使用“文件”方案URL。

在你的情况下代码应该是

    String html = readFileAsString(
       new FileInputStream("file://" + contentLocation + url) );
webView.loadDataWithBaseURL("file://" + contentLocation,html,"text/html","utf-8",null);


public static StringBuffer streamToString(InputStream stream ){

        StringBuffer fileContent = new StringBuffer("");
        try {

            int size = stream.available();
            byte[] buffer = new byte[size];
            int length;
            while ((length = stream.read(buffer)) != -1) {
                fileContent.append(new String(buffer));
            }
            stream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            try {
                stream.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

        return fileContent;
    }

希望答案有助于未来的访客

PS:上面的代码片段未编译

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM