【问题标题】:Webview AppCache not loading sourced non-html contentWebview AppCache 未加载来源的非 html 内容
【发布时间】:2012-06-28 08:18:26
【问题描述】:

我正在使用 HTML5 AppCache 创建一个可以离线运行的 Android 网络应用程序。使用 loadDataWithBaseURL() 将获取其他图像、样式表、javascript 和 iframe 的 HTML 加载到 WebView 中。不幸的是,当设备离线时,只有 iframe 中的 HTML 会从 AppCache 加载。

此时,我知道了:

  • 该内容确实存在于 AppCache 中,因为我使用 adb shell 转储了 AppCache.db 文件的内容并在其中找到了所有内容。
  • 这可能不是域问题,因为我在 loadDataWithBaseURL() 的 baseUrl 字段中指定了正确的路径。此外,如下所述,此问题的解决方法会成功且不会出现域错误。

这是一些演示代码:

public class ExampleActivity extends Activity {

   ...

  // HTML to be inserted into the Webview with loadDataWithBaseURL()
  public static final String ALL_HTML = 
      "<!DOCTYPE HTML><html>" +
      "<head><script src='sourced_js.js' " +
         "onload='console.log(\"sourced_js.js onload\");'>" +
      "</script>" +
      "<link rel='stylesheet' href='style.css' />" +   // doesn't load offline
      "</head><body>" +
      "<iframe src='manifest.html'></iframe>" +        // loads
      "<img src='android.jpg' />" +                    // doesn't load
      "<img src='android.gif' />" +                    // doesn't load
      "</body></html>";

  public void onCreate(Bundle savedInstanceState) {

    ...

    WebView webView = new WebView(context);
    webView.clearCache(true);

    WebSettings settings = webView.getSettings();
    settings.setAppCacheEnabled(true);
    settings.setJavaScriptEnabled(true);

    webView.loadDataWithBaseURL("http://my.website.com/path/to/content/",
        ALL_HTML, "text/html", "utf-8", null);
  }
}

manifest.html 只负责引用清单。它看起来像:

<html manifest="manifest.appcache">
<head></head>
<body></body>
</html>

manifest.appcache 看起来像:

CACHE MANIFEST

# Explicitly cached resources
# manifest.html automatically cached
sourced_js.js
android.jpg
android.gif
style.css

NETWORK:
*

在线时,所有内容都会加载。离线时,仅加载带有 manifest.html 的 iframe。图像、样式表和 javascript 未加载。

奇怪的是,如果我在manifest.html 中获取完全相同的静态内容(sourced_js.jsandroid.jpg、...),它们都会在设备离线时从 iframe 中的 AppCache 正确加载!好像这些其他资源必须从静态页面中获取。

任何线索为什么不会从 AppCache 加载此内容?

【问题讨论】:

    标签: java android html html5-appcache


    【解决方案1】:

    如果您没有根据规范为 Web 服务器中的 *.appcache 文件设置正确的 MIME 类型响应设置(文本/缓存清单),则 appcache 将无法工作。

    预计在桌面浏览器、移动浏览器和 iOS UIWebView 中会有相同的行为。

    【讨论】:

      猜你喜欢
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      相关资源
      最近更新 更多