【问题标题】:Android - Save data from WebViewAndroid - 从 WebView 保存数据
【发布时间】:2015-08-31 16:17:20
【问题描述】:

目前我在我的应用程序中使用WebView 来加载网页。我在WebView 中加载了以下网页网址:

http://domain_name/app/

如果用户未登录,则 URL 将重定向到另一个 URL,即登录 URL。这是登录网址:

http://domain_name/app/index.php?r=site/login

用户输入用户名和密码后,它被重定向到以下 URL:

http://domain_name/app/index.php?r=site/homepage

当用户成功登录后,主页会显示给用户。

我的问题是当应用程序从最近列表中删除时,登录数据丢失并且应用程序被重定向到登录屏幕。

我有一种方法可以解决这个问题。首先是在用户成功登录后最初给出登录页面 URL 我将 URL 更改为主页 URL 而不是登录页面 URL。

有没有其他方法可以解决这个问题,或者有什么方法可以将登录数据保存到应用程序存储中?

【问题讨论】:

    标签: android android-webview


    【解决方案1】:

    我正在回答我自己的问题,我认为这可能对其他人有帮助。

    最后我通过在内部和外部存储中缓存解决了我的问题(如果可用,将数据保存到外部存储)

    首先我创建了自定义Application 类,它在内部或外部存储中创建缓存存储路径。

    MyApplication.java

    import android.app.Application;
    import android.os.Environment;
    
    import java.io.File;
    
    public class MyApplication extends Application {
    
    protected File extStorageAppBasePath;
    protected File extStorageAppCachePath;
    
    @Override
    public void onCreate() {
        super.onCreate();
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            File externalStorageDir = Environment.getExternalStorageDirectory();
            if (externalStorageDir != null) {
                extStorageAppBasePath = new File(externalStorageDir.getAbsolutePath() +
                        File.separator + "Android" + File.separator + "data" +
                        File.separator + getPackageName());
            }
    
            if (extStorageAppBasePath != null) {
                extStorageAppCachePath = new File(extStorageAppBasePath.getAbsolutePath() +
                        File.separator + "cache");
    
                boolean isCachePathAvailable = true;
    
                if (!extStorageAppCachePath.exists()) {
                    isCachePathAvailable = extStorageAppCachePath.mkdirs();
                }
    
                if (!isCachePathAvailable) {
                    extStorageAppCachePath = null;
                }
            }
        }
    }
    
    @Override
    public File getCacheDir() {
        if (extStorageAppCachePath != null) {
            return extStorageAppCachePath;
        }
        else {
            return super.getCacheDir();
        }
    }
    

    }

    在 Android Manifest 文件中,我在 application 标签下提供了以下权限和应用程序名称。

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
     <application
            android:name=".MyApplication"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    ........................................
    </application>
    

    如果缓存可用,则最终启用缓存以从存储中加载。

    mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    

    【讨论】:

      【解决方案2】:

      在加载页面之前使用:

      CookieManager.getInstance().setAcceptCookie(true);
      

      如果它不起作用,请阅读 CookieSyncManager 以同步 RAM 和永久存储中的浏览器 cookie 存储。

      对于 Lollipop 及以上,这应该足够了:

      CookieManager.getInstance().setAcceptThirdPartyCookies(true);
      

      【讨论】:

      • 我试过了。但它不起作用。感谢您的帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      • 2010-11-25
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      相关资源
      最近更新 更多