Android WebView加载缓存页面问题:Cache Manifest无法生效求助
Hey there, let's tackle this WebView offline caching issue step by step. I've looked through your code and spotted a few critical problems that are blocking the Cache Manifest from working on Android WebView. Here's how to fix them:
1. Fix the Network Check Logic (Most Important!)
Right now, your code closes the activity immediately when there's no internet connection. That means the WebView never even gets initialized to load cached content. Let's remove that finish() call and set up the WebView regardless of network status:
// Update the network check section to not close the activity if (!DetectConnection.checkInternetConnection(this)) { Toast.makeText(getApplicationContext(), "No Internet Connection! Loading cached content...", Toast.LENGTH_LONG).show(); } // Move WebView initialization OUT of the else block - we need it even offline mWebview = (WebView) findViewById(R.id.webview1); WebSettings webSettings = mWebview.getSettings(); // ... rest of your WebView setup code ...
2. Correct WebView Cache Settings
Your current cache settings are disabling the app cache entirely, which breaks the Cache Manifest functionality. Update these lines:
// Enable app cache (required for Cache Manifest to work) webSettings.setAppCacheEnabled(true); // Set a valid cache path (Android requires a specific directory for cached files) webSettings.setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath()); // Set cache mode to prioritize cached content when offline webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); // Keep this enabled (already in your code, but critical for cache access) webSettings.setAllowFileAccess(true);
Quick breakdown of these changes:
setAppCacheEnabled(true): Turns on the WebView's application cache system, which is mandatory for Cache Manifest support.setAppCachePath(...): Android requires you to specify a valid directory for storing cached files; using the app's default cache directory is secure and compliant.setCacheMode(LOAD_CACHE_ELSE_NETWORK): Tells the WebView to use cached content if available (even offline). Only if the cache doesn't have the content will it try to load from the network.
3. Improve Error Handling
Your current onReceivedError tries to load a remote URL when something fails, which will just fail again offline. Update it to handle offline scenarios gracefully:
@Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { if (!DetectConnection.checkInternetConnection(MainActivity.this)) { Toast.makeText(MainActivity.this, "Offline - showing cached content", Toast.LENGTH_SHORT).show(); // Optional: Load a local offline fallback page from your assets if you have one // view.loadUrl("file:///android_asset/offline.html"); } else { super.onReceivedError(view, errorCode, description, failingUrl); Toast.makeText(MainActivity.this, "Failed to load page: " + description, Toast.LENGTH_SHORT).show(); } }
4. Double-Check Permissions
Make sure your AndroidManifest.xml includes these permissions (you might already have them, but it's worth verifying):
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
5. Quick Frontend Sanity Check
Since it works in Chrome, your Cache Manifest is probably correct, but just to confirm:
- Your server serves the manifest file with the correct MIME type (
text/cache-manifest). - All resources listed in the manifest are accessible and get cached when the app is online.
- The manifest's
CACHE,NETWORK, andFALLBACKsections are properly configured.
Testing Steps
After making these changes, test the offline functionality like this:
- Open the app with an internet connection first (so the WebView can cache all required content).
- Turn off your device's internet connection.
- Re-open the app — it should load the cached content instead of showing the
ERR_NAME_NOT_RESOLVEDerror.
内容的提问来源于stack exchange,提问作者Observer

