Android webview & localStorage


Tengo un problema con una vista web que puede acceder al localStorage mediante una aplicación HTML5. Prueba.archivo html me informa que local el almacenamiento no es compatible con mi navegador (es decir. el webview). Si tienes alguna sugerencia..

package com.test.HelloWebView; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.webkit.WebChromeClient; 
import android.webkit.WebSettings; 
import android.webkit.WebStorage; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
public class HelloWebView extends Activity { 
WebView webview; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    webview = (WebView) findViewById(R.id.webview); 
    webview.getSettings().setJavaScriptEnabled(true); 
    webview.setWebViewClient(new HelloWebViewClient()); 
    webview.loadUrl("file:///android_asset/test.html"); 
    WebSettings settings = webview.getSettings(); 
    settings.setJavaScriptEnabled(true); 
    settings.setDatabaseEnabled(true); 
    String databasePath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); 
    settings.setDatabasePath(databasePath);
    webview.setWebChromeClient(new WebChromeClient() { 
    public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { 
            quotaUpdater.updateQuota(5 * 1024 * 1024); 
        } 
    }); 
} 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { 
        webview.goBack(); 
        return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 
private class HelloWebViewClient extends WebViewClient { 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        view.loadUrl(url); 
        return true; 
    } 
}
} 
Author: Xcodian Solangi, 2011-05-05

6 answers

Faltaba lo siguiente:

settings.setDomStorageEnabled(true);
 424
Author: Thomas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-15 14:36:35

El método SetDatabasePath() fue obsoleto en el nivel de API 19. Le aconsejo que utilice la configuración regional de almacenamiento de esta manera:

webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
}
 34
Author: mr.boyfox,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-02 06:27:31

También he tenido problemas con la pérdida de datos después de reiniciar la aplicación. Agregar esto ayudó:

webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
 22
Author: iTake,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-21 09:36:53

Una solución que funciona en mi Android 4.2.2, compilada con build target Android 4.4 W:

WebSettings settings = webView.getSettings();
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    File databasePath = getDatabasePath("yourDbName");
    settings.setDatabasePath(databasePath.getPath());
}
 11
Author: AngryWolf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-26 09:33:41

Si su aplicación utiliza varias vistas web, todavía tendrá problemas : localStorage no se comparte correctamente en todas las vistas web.

Si desea compartir los mismos datos en varias vistas web, la única manera es repararlos con una base de datos java y una interfaz javascript.

Esta página en github muestra cómo hacer esto.

Espero que esta ayuda!

 3
Author: Guillaume Gendre,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-13 16:01:07

Si tiene varias vistas web, localstorage no funciona correctamente.
dos sugerencias:

  1. usando base de datos java en lugar de webview localstorage que "@Guillaume Gendre " explicó.(por supuesto que no funciona para mí)
  2. el almacenamiento local funciona como json,por lo que los valores se almacenan como "clave:valor" .puede agregar su id único del navegador a su clave y el uso normal de android localstorage
 0
Author: MHP,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-26 09:54:09