【问题标题】:Android 4.0.1 breaks WebView HTML 5 local storage?Android 4.0.1 打破了 WebView HTML 5 本地存储?
【发布时间】:2012-01-13 12:40:31
【问题描述】:

我有一个简单的 html5 测试页面,它使用 LocalStorage 来显示/保存/重新显示一段数据。

此代码在 Android 2.3.x 中完美运行,但 记录 4.0.1 中 html 的第 18 行出现异常,这是第一个 localStorage.getItem() 调用,此时 JS 停止。

例外:Uncaught Error: SECURITY_ERR: DOM Exception 18 at /data/data/my.app.name/app_htmlData:18 我也尝试将数据库路径设置为getCacheDir(),结果相同。

String htmlContent = "HTML content listed below";    
File sharedDir = getActivity().getDir("htmlData", Context.MODE_PRIVATE);
WebView browser = (WebView)v.findViewById(R.id.wvBrowser);

browser.setWebChromeClient(new WebChromeClient(){
    public void onExceededDatabaseQuota(String url, String databaseIdentifier, long  currentQuota, long estimatedSize,   long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { 
            quotaUpdater.updateQuota(estimatedSize * 2); 
        }
    });       
browser.setWebViewClient(new WebViewClient(){
    @Override
    public void onPageFinished(WebView view, String url){

        view.loadUrl("javascript:doTest()");

    });

browser.getSettings().setDatabaseEnabled(true);
browser.getSettings().setDatabasePath(sharedDir.getPath());
browser.getSettings().setDomStorageEnabled(true);
browser.loadDataWithBaseURL(mSharedDir.getPath(), 
            htmlContent, 
            "text/html", 
            "utf-8", 
            null);

页面正在渲染的HTML如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Simple localStorage test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">

        function doTest() {
            $('#stuff').append('<p>reading</p>');
            var item = read();

            $('#stuff').append('<p>writing</p>');
            localStorage['bar'] = new Date().toUTCString();

            $('#stuff').append('<p>&nbsp;</p><p>reading again</p>');
            read();
        }
        function read() {
            var item = localStorage.getItem('bar');
            if (item == null || (item == undefined)) {
                item = '';
            }
            $('#stuff').append('<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item: ' + item + '</p>');

         return item;
        }
    </script>
</head>
<body>
    <p>-Simple localStorage test-</p>
    <div id="stuff"></div>
</body>
</html>

来源可用here

【问题讨论】:

  • 记录的异常是什么?
  • 抱歉,这很有用 - 已添加到问题中
  • 这很奇怪。如果您有一个完整的示例项目,您可以打包并上传到某个地方,我想看看它。
  • 完成,我注意到在 4.0 更改日志中潜伏着这样的内容:“WebKit 更新到版本 534.30”
  • 我明白你所看到的,除了这个问题,我没有任何解释:code.google.com/p/android/issues/detail?id=16175

标签: android html jquery-mobile webview local-storage


【解决方案1】:

通过与 Google 工程师的讨论,他们似乎已决定 file:// 方案不安全。

解决此问题的方法是执行以下操作

browser.loadDataWithBaseURL("http://www.example.com", 
            htmlContent, 
            "text/html", 
            "utf-8", 
            null);

【讨论】:

  • 非常感谢您的回答,它也解决了我对未捕获错误的查询:SECURITY_ERR: DOM Exception 18:2。
【解决方案2】:

对于低于 4.4 的 android 版本,将数据加载到以文件方案作为目录的 webview 中:

browser.loadDataWithBaseUrl("file:///android_asset/", html, "text/html", "UTF-8", null);

不适用于 localStorage。如果我添加文件名,它确实适用于旧操作系统版本

browser.loadDataWithBaseUrl("file:///android_asset/test.html", html, "text/html", "UTF-8", null);

【讨论】:

  • 难以置信...在我的情况下,该应用程序使用 API 12,从 Android 3.1 开始运行,没有将 setAllowUniversalAccessFromFileURLs (stackoverflow.com/questions/19379392/…) 设置为 true,因此,4.2 上的空白页。有了它现在一切都好。
猜你喜欢
  • 2015-07-01
  • 2012-03-21
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-10
相关资源
最近更新 更多