简体   繁体   English

Android 4.4 在 onReceivedError 中为 WebView 返回 ERR_CACHE_MISS 错误

[英]Android 4.4 giving ERR_CACHE_MISS error in onReceivedError for WebView back

I have a webview in my Layout.我的布局中有一个 webview。 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.现在我想控制 webview 的后退导航。 I placed this code in Activity.我将此代码放在活动中。

    @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.我还使用 WebView 设置了一个 WebViewClient。 When I going back using back button it is working fine for any version 4.4.当我使用后退按钮返回时,它适用于任何 4.4 版本。 But when I am trying in Android 4.4, it is coming back fine from details page to listing page.但是当我在 Android 4.4 中尝试时,它从详细信息页面到列表页面都恢复正常。 But as soon as I am trying to go back again, its throwing error code -1 and ERR_CACHE_MISS in description.但是一旦我试图再次返回,它就会在描述中抛出错误代码 -1 和 ERR_CACHE_MISS。 No page is displayed.不显示页面。

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 中解决这个问题?

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:这是相关的位,重新散列一点:

  1. Joe fills in an order form with his credit card information乔用他的信用卡信息填写订单
  2. 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.服务器处理该信息并返回一个在标头中标有no-cache的确认/收据页面,这意味着它将始终从服务器请求。
  3. Joe goes to another page.乔转到另一页。
  4. Joe clicks back because he wants to double check something, taking him to the confirmation page. 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.确认页面被标记为no-cache ,因此必须再次从服务器请求。 But to show the same page correctly, the same data that was passed the first time needs to get sent again.但是为了正确显示相同的页面,第一次传递的相同数据需要再次发送。

This results in Joe getting billed twice, since a new request is being made with the same information as last time.这导致 Joe 被计费两次,因为使用与上次相同的信息发出新请求。 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 发现他的帐户上有两项费用并且他家门口有一对额外的帐篷时,他将不会是一个快乐的露营者。

It seems this situation was common enough that it is now a standard error across most browsers, and apparently, newer versions of Android.这种情况似乎很常见,现在它是大多数浏览器的标准错误,而且显然是较新版本的 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 ).该错误实际上源自 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.这是 Android 4.4 警告您正在发生的事情的方式。 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).错误的确切触发实际上是无法从缓存中处理请求(在这种情况下, 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.但从您的应用程序的角度来看,主要问题是, onReceiveError是 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.所以你没有机会允许重新发送,你不能给用户那个选项,不像谷歌浏览器那样。

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"/>

Use

if (Build.VERSION.SDK_INT >= 19) {
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    }

It will fix ERR_CACHE_MISS in the WebView.它将修复 WebView 中的 ERR_CACHE_MISS。 Maybe you will need to change it to SDK_INT == 19 after some Lollipop WebView updates, but it works for now.也许您需要在一些 Lollipop WebView 更新后将其更改为 SDK_INT == 19,但它现在有效。

您的 andriodManifest.xml 文件中的此权限

 <uses-permission android:name="android.permission.INTERNET"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM