在Android的WebView中,`DomStorage`、`Database`、`AppCache`之间有什么区别?

发布于 2025-01-08 10:38:10 字数 1310 浏览 6 评论 0原文

Android的WebView中,DomStorageDatabaseAppCache有什么区别?

我对这三个设置感到困惑。现在我需要一个可以在 JavaScript 中使用 localStorage 的 WebView 实例。

这是一个例子:

        if (localStorage['avatar_user_' + nick]) {
        obj.src = localStorage['avatar_user_' + nick];
    } else {
        var url = avatar_path + nick + '/avatar.jpg';
        new Oauth().img("jpeg", url,
            function(data) {
                if (data != '') {
                    obj.src = data;
                    localStorage['avatar_user_' + nick] = data;
                }
            }, function() {}
        );
    }

在上面的代码中,我确实从网络上获取了图像,下次我需要它时将从 localStorage 加载它,但存储只持续与应用程序的生命周期一样长。

如果我重新启动我的应用程序,那么图像将再次从网络加载。

我按如下方式设置 WebView:

    mWebSettings.setAppCacheEnabled(true);
    mWebSettings.setDomStorageEnabled(true);
    mWebSettings.setDatabaseEnabled(true);
    mWebSettings.setDatabasePath("file:///data/data/com.xxx/databases");

但是当我向 localStorage 添加内容时,我可以看到 file:///data/data/com.xxx/databases 中的文件保持不变。

所以我的问题是如何让我的网页存储数据并在应用程序关闭后保留它们?

如果我在应用程序中使用多个WebView并指向DatabasePath,这是否重要?到同一个文件夹?

如果您能告诉我 DomStorage、Database 和 AppCache 之间的区别那就更好了!

In Android's WebView, what's the difference between DomStorage, Database and AppCache?

I'm confused about these three settings. Now I need a instance of WebView that I can use localStorage in JavaScript.

Here is an example:

        if (localStorage['avatar_user_' + nick]) {
        obj.src = localStorage['avatar_user_' + nick];
    } else {
        var url = avatar_path + nick + '/avatar.jpg';
        new Oauth().img("jpeg", url,
            function(data) {
                if (data != '') {
                    obj.src = data;
                    localStorage['avatar_user_' + nick] = data;
                }
            }, function() {}
        );
    }

In the above code I really get the image from the web, and it will be loaded from the localStorage the next time I need it, but the storage only lasts as long as the app's lifetime.

If I restart my app, then the image will be loaded from the web again.

I setup my WebView as follows:

    mWebSettings.setAppCacheEnabled(true);
    mWebSettings.setDomStorageEnabled(true);
    mWebSettings.setDatabaseEnabled(true);
    mWebSettings.setDatabasePath("file:///data/data/com.xxx/databases");

but I can see the files in file:///data/data/com.xxx/databases remain unchanged when I'm adding something into localStorage.

So my question is how to make my webpages store data and keep them after the app is closed?

And does it matters if I use multiple WebView in a app and point the DatabasePath to the same folder?

And if you can tell me the difference between DomStorage, Database and AppCache it would be so much better!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

早乙女 2025-01-15 10:38:10

我不太确定,但 DomStorage 似乎是 WebStorage 的同义词。这是一种在客户端站点上将数据存储为键=值对的技术。
http://diveintohtml5.info/storage.html

Database 表示将启用客户端 SQL 数据库.
http://www.html5rocks.com/en/tutorials/webdatabase/todo/

AppCache 是一种使整个网站可供离线使用的技术。
http://appcachefacts.info/

I'm not hundert percent sure, but DomStorage seems like an synonym for WebStorage. This is a technique to store data on client site as key=value pairs.
http://diveintohtml5.info/storage.html

Database means that the client-side SQL database will be enabled.
http://www.html5rocks.com/en/tutorials/webdatabase/todo/

And AppCache is a technique to make whole websites available for offline usage.
http://appcachefacts.info/

酷到爆炸 2025-01-15 10:38:10

问题解决了!我刚刚犯了一个错误,我使用 file:///data/data/com.xxx/databases 作为数据库路径,它应该只是 /data/data/com.xxx/没有 file:// 的数据库

仍在等待 DomStorage、Database、AppCache 之间差异的答案。

The problem is solved! I just made a mistake that I use file:///data/data/com.xxx/databases as the database path, it should be just /data/data/com.xxx/databases without the file:// .

Still waiting for the answers to the different between DomStorage, Database, AppCache.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文