Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android WebView HTML5访问数据库问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在一个应用程序工作
嵌入WebView并显示 @L_675_2@页面.

在Android chrome Browser中打开html页面时,数据库创建正常.

但是当我运行这个应用程序时,数据库无法创建.
看来html页面无法在WebView上创建数据库.

有谁知道为什么?

这是我的活动代码

public class efan_NewsReader extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceStatE) {
    super.onCreate(savedInstanceStatE);
    setContentView(R.layout.main);        
    WebView myWebView=(WebView)findViewById(R.id.my_webview);
    myWebView.setWebViewClient(new WebViewClient());
    WebSetTings setTings = myWebView.getSetTings();  
    setTings.setJavaScriptEnabled(true);
    setTings.setDomStorageEnabled(true);
    setTings.setDatabaseEnabled(true);
    setTings.setAppCacheEnabled(false);        

    myWebView.loadUrl("http://10.10.35.47:8080/html5test/test.htm");

}}

这是我的HTML5页面代码

<html manifest="mymanifest.manifest">
    <head>
<Meta http-equiv="Content-Type" content="text/html; content="no-cache" charset=utf-8" />
<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>

<script>
$(document).ready(function(){       

    databasetest();
});

function databasetest(){


    //open database
     var db = openDatabase('mydb','1.0','Test DB',2 * 1024 * 1024);  

      db.transaction(function (tX) {            
      tx.executesql('create table IF NOT EXISTS testHtml (id unique,contentText)');
      tx.executesql('INSERT INTO testHtml (contentText) VALUES ("insert data test!")');  
       });  

     db.transaction(function(tx){           
     tx.executesql('SELECT * FROM testHtml',[],function(tx,result){
            var len=result.rows.length;
            var msg = "<p>Found rows: " + len + "</p>";  
             $("#tesTinfo").append(msg);
        },null);
     });    


}

</script>


</head>
<body>
    <div>here is test info:</div>
    <div id="tesTinfo"></div>
</body>

解决方法

试试:

setTings.setDatabaseEnabled(true);
String dbPath = this.getApplicationContext().getDir("database",Context.MODE_PRIVATE).getPath();
setTings.setDatabasePath(dbPath);

在WebChromeClient中:

@H_109_17@myWebView.setWebChromeClient(new WebChromeClient() { @Override public void onExceededDatabaseQuota(String url,String databasEIDentifier,long currentQuota,long estimatedSize,long @R_421_10586@lUsedQuota,WebStorage.Quotaupdater quotaupdater) { quotaupdater.updateQuota(estimatedSize * 2); } }

应该没问题;)

大佬总结

以上是大佬教程为你收集整理的android WebView HTML5访问数据库问题全部内容,希望文章能够帮你解决android WebView HTML5访问数据库问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。