Android 4.4 在 onReceivedError 中为 WebView 返回 ERR_CACHE_MISS 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25664146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android 4.4 giving ERR_CACHE_MISS error in onReceivedError for WebView back
提问by dev_android
I have a webview in my Layout. By default, a search form is opened in it. On search, a listing section appears below the search form. If any link in the list is clicked, the details page opened. Now I want to controlled the back navigation for the webview. I placed this code in Activity.
我的布局中有一个 webview。默认情况下,会在其中打开搜索表单。在搜索时,搜索表单下方会出现一个列表部分。如果单击列表中的任何链接,将打开详细信息页面。现在我想控制 webview 的后退导航。我将此代码放在活动中。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d("TYPE", TYPE);
WebView myWebView = null;
if (TYPE.equalsIgnoreCase("REPORT_ACTIVITY"))
myWebView = reportView;
if (TYPE.equalsIgnoreCase("FEEDBACK_ACTIVITY"))
myWebView = feedbackView;
if (myWebView != null)
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up
// to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
private WebViewClient webViewClient = new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.d("onPageStarted", "onPageStarted");
loadProgressBarBox.setVisibility(View.VISIBLE);
//view.setVisibility(View.GONE);
}
public void onPageFinished(WebView view, String url) {
Log.d("onPageFinished", "onPageFinished");
loadProgressBarBox.setVisibility(View.GONE);
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.d("Error", "Error code: " + errorCode + "/" + description);
}
}
I have also set a WebViewClient with the WebView. When I going back using back button it is working fine for any version 4.4. But when I am trying in Android 4.4, it is coming back fine from details page to listing page. But as soon as I am trying to go back again, its throwing error code -1 and ERR_CACHE_MISS in description. No page is displayed.
我还使用 WebView 设置了一个 WebViewClient。当我使用后退按钮返回时,它适用于任何 4.4 版本。但是当我在 Android 4.4 中尝试时,它从详细信息页面到列表页面都恢复正常。但是一旦我试图再次返回,它就会在描述中抛出错误代码 -1 和 ERR_CACHE_MISS。不显示页面。
09-04 06:59:05.666: D/Error(1102): Error code: -1/net::ERR_CACHE_MISS
How to solve this problem in Android 4.4?
如何在 Android 4.4 中解决这个问题?
回答by Selali Adobor
This error actually stems from outside of your application in most cases (occasionally it's just a missing INTERNET
permission, but that doesn't sound like the case here).
在大多数情况下,此错误实际上源于您的应用程序外部(有时它只是缺少INTERNET
权限,但听起来不像这里的情况)。
I was typing out an explanation, but found a much more straightforward example that doubles as an explanation in this answer to another question. Here's the relevant bits, re-hashed a little:
我正在输入一个解释,但发现了一个更直接的例子,它在另一个问题的答案中兼作解释。这是相关的位,重新散列一点:
- Joe fills in an order form with his credit card information
- The server processes that information and returns a confirmation/receipt page that's marked with
no-cache
in the header, meaning it will always be requested from the server. - Joe goes to another page.
- Joe clicks back because he wants to double check something, taking him to the confirmation page.
- 乔用他的信用卡信息填写订单
- 服务器处理该信息并返回标
no-cache
头中标有 的确认/收据页面,这意味着它将始终从服务器请求。 - 乔转到另一页。
- Joe 回击,因为他想再次检查一些东西,将他带到确认页面。
The problem arises from that last step. The confirmation page was marked with no-cache
, so it has to be requested from the server again. But to show the same page correctly, the same data that was passed the first time needs to get sent again.
问题出在最后一步。确认页面标有no-cache
,因此必须再次从服务器请求。但是为了正确显示相同的页面,第一次传递的相同数据需要再次发送。
This results in Joe getting billed twice, since a new request is being made with the same information as last time. Joe will not be a happy camper when he finds two charges on his account and an extra pair of tents on his doorstep.
这导致 Joe 被计费两次,因为使用与上次相同的信息发出新请求。当 Joe 发现他的帐户上有两项费用并且他家门口有一对额外的帐篷时,他将不会是一个快乐的露营者。
It seems this situation was common enough that it is now a standard error across most browsers, and apparently, newer versions of Android. The error actually originates from Chromium, which is why you'll see the same error in Google Chrome, and why you only see it in 4.4 (which introduced a new version of the WebView based on Chromium).
这种情况似乎很常见,现在它是大多数浏览器的标准错误,而且显然是较新版本的 Android。该错误实际上源自 Chromium,这就是为什么您会在 Google Chrome 中看到相同的错误,以及为什么您只会在 4.4(它引入了基于 Chromium的 WebView 的新版本)中看到它。
In fact, you have actually probably seen it before, it's the message that shows up in most browsers warning you with something along the lines of "To refresh this page, the browser will have to resend data...yada yada yada".
事实上,您之前可能已经看过它,它是大多数浏览器中显示的消息,警告您“要刷新此页面,浏览器将不得不重新发送数据...yada yada yada”。
This is Android 4.4's way warning you of what's going on. How to fix it really depends on what you're connecting to, but if you search for this situation, you'll find that it's fairly common, and has fixes. The exact trigger of the error is actually when the request can't be serviced from cache (in this case, no-cache
is causing that).
这是 Android 4.4 警告您正在发生的事情的方式。如何修复它实际上取决于您连接的对象,但是如果您搜索这种情况,您会发现它很常见,并且有修复方法。错误的确切触发实际上是无法从缓存中处理请求(在这种情况下,no-cache
是导致这种情况的原因)。
Depending on the nature of the request, maybe no-cache
isn't actually needed.
根据请求的性质,可能no-cache
实际上并不需要。
But from your application's perspective, the main problem is, onReceiveError
is a sort of "last resort" for the WebView. Errors you get there have propagated from underlying system. And once you end up there, you can't continue the page load as it stands. So you don't have a chance to allow that resend, and you can't give the user that option, unlike, say Google Chrome does.
但从您的应用程序的角度来看,主要问题是,onReceiveError
WebView 是一种“最后的手段”。你在那里得到的错误是从底层系统传播的。一旦你到达那里,你就不能继续按原样加载页面。所以你没有机会允许重新发送,你不能给用户那个选项,不像谷歌浏览器那样。
回答by codebyjames
I ran into the same issue because in my manifest folder I had the Internet permission capitalized:
我遇到了同样的问题,因为在我的清单文件夹中,我的 Internet 权限大写:
I had (error)
我有(错误)
<uses-permission android:name="ANDROID.PERMISSION.INTERNET"/>
Should have (no error)
应该有(没有错误)
<uses-permission android:name="android.permission.INTERNET"/>
回答by romashko_o
Use
用
if (Build.VERSION.SDK_INT >= 19) {
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
It will fix ERR_CACHE_MISS in the WebView. Maybe you will need to change it to SDK_INT == 19 after some Lollipop WebView updates, but it works for now.
它将修复 WebView 中的 ERR_CACHE_MISS。也许您需要在一些 Lollipop WebView 更新后将其更改为 SDK_INT == 19,但它现在有效。
回答by Sai Gopi N
this permission in your andriodManifest.xml file
您的 andriodManifest.xml 文件中的此权限
<uses-permission android:name="android.permission.INTERNET"/>