Webview AppCache未加载源非HTML内容 [英] Webview AppCache not loading sourced non-html content
问题描述
我正在使用HTML5 AppCache创建一个可以离线运行的Android网络应用。使用loadDataWithBaseURL()将使用其他图像,样式表,javascript和iframe的HTML加载到WebView中。不幸的是,当设备离线时,只有来自iframe的HTML从AppCache加载。
I'm using HTML5 AppCache to create an Android web app which can function offline. HTML which sources additional images, stylesheets, javascript and iframes is loaded into a WebView with loadDataWithBaseURL(). Unfortunately, when the device is offline only the HTML sourced in iframes is loaded from 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
只负责引用清单。它看起来像:
manifest.html
is just responsible for referencing the manifest. It looks like:
<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。
When online, all content loads. When offline, only the iframe with manifest.html
loads. The images, stylesheet and javascript are not loaded.
奇怪的是,如果我采购完全相同的静态内容( sourced_js.js
, android.jpg
,...)在 manifest.html
中,它们都是从AppCache中加载的设备离线时iframe正确!好像这些其他资源必须从静态页面二次获取。
Strangely enough, if I source the exact same static content (sourced_js.js
, android.jpg
, ...) in manifest.html
, they all load from AppCache in the iframe properly when the device is offline! As if these other resources have to be sourced secondarily from a static page.
为什么不能从AppCache加载此内容的任何线索?
Any clue why this content won't be loaded from the AppCache?
推荐答案
如果没有为* .appcache文件中的* .appcache文件设置正确的MIME类型响应设置(text / cache-manifest),appcache应该不起作用Web服务器,根据规范。
The appcache should not work if you don't set the correct MIME type response setting (text/cache-manifest) for *.appcache files in your web server, according to the specs.
桌面浏览器,移动浏览器和iOS UIWebView预计会出现同样的行为。
The same behavior is expected in desktop browsers, mobile browsers and iOS UIWebView.
这篇关于Webview AppCache未加载源非HTML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!