我正在使用HTML5 AppCache創建可以脫機工作的Android Web應用程序。使用loadDataWithBaseURL()將附加圖像,樣式表,JavaScript和iframe源代碼的HTML加載到WebView中。不幸的是,當設備處於離線狀態時,僅從AppCache中加載源自iframe的HTML。Webview AppCache未加載源非HTML內容
在這一點上,我知道:
- 這項內容在應用程序緩存的存在,因爲我甩了AppCache.db與亞行shell文件的內容和發現所有的內容在那裏。
- 這可能不是一個域問題,因爲我在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.js
,android.jpg
,...),他們從應用程序緩存所有負載在iframe當正確的設備離線!就好像這些其他資源必須從靜態頁面進行二次採購。
任何線索爲什麼這個內容不會從AppCache中加載?