android webview 调试 缓存 踩坑

本文详细介绍了在Android开发中使用WebView遇到的各种问题及解决方法,包括调试技巧,以及如何配置和管理WebView的离线缓存策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.各种踩坑

1.WebView必不可少的一句

webView.setWebViewClient(new WebViewClient());

2.http和https混合支持

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 

    webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

}

3.添加 Cookie

if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) CookieSyncManager.createInstance(context); 

 CookieManager cookieManager = CookieManager.getInstance(); 

cookieManager.setAcceptCookie(true);

cookieManager.setCookie(url, cookie);

4.删除 Cookie

CookieManager cookieManager = CookieManager.getInstance(); cookieManager.removeAllCookie();

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

cookieManager.flush(); 

}

else{ 

CookieSyncManager.createInstance(Application.getInstance()); 

CookieSyncManager.getInstance().sync();

 }

2.调试模式

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

android.webkit.WebView.setWebContentsDebuggingEnabled(true);

WebView.setWebContentsDebuggingEnabled(true);

}

3.离线缓存

if (NetworkUtil.isConnected(mContext))

    mWebView.getSettings().setCacheMode(LOAD_DEFAULT);

else

    mWebView.getSettings().setCacheMode(LOAD_CACHE_ELSE_NETWORK);

webSettings.setDatabaseEnabled(true);

webSettings.setDomStorageEnabled(true);

String cacheDirPath = g

etFilesDir().getAbsolutePath() +"/webCache";

mWebView.getSettings().setDatabasePath(cacheDirPath);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值