2012-06-20 71 views
1

這是我的問題。我試圖從我的Web應用程序本地存儲數據到HTML5數據庫。事情是,我嘗試用我的JavaScript打開的數據庫返回null。Android WebView數據庫存儲返回null

下面是Android代碼:

public class NotepadActivity extends Activity { 
WebView main_view = null; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //Get rid of the android title bar 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.main); 

     main_view = (WebView) findViewById(R.id.main_webview); 
     WebSettings settings = main_view.getSettings(); 

     settings.setJavaScriptEnabled(true); 
     settings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/"); 
     settings.setDomStorageEnabled(true); 

     main_view.loadUrl("file:///android_asset/index.html"); 

     main_view.setWebViewClient(new WebViewClient() { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       return false; 
      } 
      @Override 
      public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) { 
       Toast.makeText(NotepadActivity.this, "Error: " + description , Toast.LENGTH_LONG).show(); 
      } 
     }); 

     main_view.setWebChromeClient(new WebChromeClient() { 
       public void onConsoleMessage(String message, int lineNumber, String sourceID) { 
       Log.d("Notepad", message + " -- From line " 
           + lineNumber + " of " 
           + sourceID); 
       } 
     }); 
    } 

    public void onExceededDatabaseQuota(String url, String 
      databaseIdentifier, long currentQuota, long estimatedSize, long 
      totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { 
         quotaUpdater.updateQuota(estimatedSize * 2); 
      } 
} 

及相關的JavaScript:

 /* DB and result values */ 
    DB_name: "NotepadDB", 
    DB_version: "1", 
    DB_displayName: "Notepad Database", 
    DB_sizeEstimate: 5 * 1024 * 1024, 
    DB: null, 

.... 



    /* Open a connection to the DB (or create if it doesn't exist) */ 
    openConnection: function() { 
     App.log("App > DB > openConnection > Attempting to access database"); 
     App.val.DB = window.openDatabase(App.val.DB_name, App.val.DB_version, 
         App.val.DB_displayName, App.val.DB_sizeEstimate, App.DB.initializeDatabase); 
    }, 

    /* Only called when the database is initially created */ 
    initializeDatabase: function(database) { 
     App.val.DB = database; 

     //Create the required table 
     App.val.DB.transaction(
      function (tx) {tx.executeSql('CREATE TABLE ' + App.schema.tableName + ' (' + App.schema.ID+ ' int unique, ' + App.schema.title + ' text, ' + App.schema.content + ' text, ' + App.schema.date + ' datetime)', 
       [], 
       function (tx, res) { 
        App.log("App > DB > intializeDatabase > Table Created Successfully"); 
        App.DB.loadAllNotes(); 
       }, 
       function (tx, err) { 
        App.log("App > DB > intializeDatabase > ERROR - Table creation failed - code: " + err.code + ", message: " + err.message); 
        App.error("Unable to create database. Please try again later."); 
       }); 
      } 
     ); 
    }, 

    loadAllNotes: function() { 
     if(App.val.DB != null) { 
      //Do some stuff 
     } else { 
      App.log("App > DB > loadAllNotes > Database was NULL"); 
     } 
    } 

在我的logcat日誌我看到 App.log(「應用> DB> loadAllNotes>數據庫空值」); 正在執行。

有什麼我在這裏失蹤?爲什麼數據庫爲空?

回答

1

解決了它。問題是我忽略了下面的代碼:

 public void onExceededDatabaseQuota(String url, String 
     databaseIdentifier, long currentQuota, long estimatedSize, long 
     totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { 
        quotaUpdater.updateQuota(estimatedSize * 2); 
     } 

這個功能應該是main_view.setWebChromeClient(new WebChromeClient() {函數中,因爲它需要重寫。 D'哦!

+0

哇!我甚至不知道我必須爲WebSQL設置'onExceededDatabaseQuota'才能工作。這節省了我很多時間,謝謝。 – Maarten

0

創建SQlite數據庫文件並將其放入Assets目錄並在第一次運行應用程序時將該文件加載到SDcard中會更容易。 U可以使用db編輯器的FireFox addon。這將需要在您的應用程序少得多的代碼。您可以使用我發佈的here on SO的方法。只需使用.sql文件而不是txt文件 - 這就是我用我的應用程序所做的。

+0

我會考慮到這一點,但這個應用程序正從另一個平臺移植過來,所以我寧願讓代碼保持相同。使更新這兩個應用程序更容易,而不必爲每個應用程序編寫單獨的代碼。 – kz3

+0

很酷,我明白。 – CelticParser