Domanda

Here is my problem. I am trying to store data from my web app locally in an HTML5 database. Thing is, the database I try to open with my javascript returns null.

Here is the Android code:

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);
            } 
}

And the relevant 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");
        }
    }

In my LogCat log I am seeing App.log("App > DB > loadAllNotes > Database was NULL"); is executing.

Is there something I am missing here? Why is the database null?

È stato utile?

Soluzione

Solved it. The problem was I overlooked the following code:

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

This function should be within the main_view.setWebChromeClient(new WebChromeClient() { function, as it needs to override. D'oh!

Altri suggerimenti

It would be easier to create a SQlite database file and put it into your Assets directory and load the file onto the SDcard when the app is first ran. U can use a FireFox addon for the db editor. This will require a lot less code in your app. You can use my methods I posted here on SO. Just use the .sql file instead of a txt file - This is what I do with my apps .

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top