Android 4.4 在 onReceivedError 中为 WebView 返回 ERR_CACHE_MISS 错误 [英] Android 4.4 giving ERR_CACHE_MISS error in onReceivedError for WebView back

查看:64
本文介绍了Android 4.4 在 onReceivedError 中为 WebView 返回 ERR_CACHE_MISS 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局中有一个 webview.默认情况下,会在其中打开搜索表单.在搜索时,搜索表单下方会出现一个列表部分.如果单击列表中的任何链接,将打开详细信息页面.现在我想控制 webview 的后退导航.我将此代码放在 Activity 中.

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.

    @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);
       }

}

我还为 WebView 设置了一个 WebViewClient.当我使用后退按钮返回时,它适用于任何 4.4 版本.但是当我在 Android 4.4 中尝试时,它从详细信息页面到列表页面都恢复正常.但是一旦我试图再次返回,它就会在描述中抛出错误代码 -1 和 ERR_CACHE_MISS.不显示页面.

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.

09-04 06:59:05.666: D/Error(1102): Error code: -1/net::ERR_CACHE_MISS

Android 4.4 如何解决这个问题?

How to solve this problem in Android 4.4?

推荐答案

在大多数情况下,此错误实际上源于您的应用程序外部(有时它只是缺少 INTERNET 权限,但这不会听起来像这里的情况).

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).

我正在输入一个解释,但在另一个问题的答案.这是相关的位,重新散列一下:

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. 乔用他的信用卡信息填写订单
  2. 服务器处理该信息并返回一个确认/收据页面,该页面在标头中标有 no-cache,这意味着它将始终从服务器请求.
  3. Joe 转到另一个页面.
  4. Joe 回击,因为他想再次检查一些东西,将他带到确认页面.
  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.
  3. Joe goes to another page.
  4. Joe clicks back because he wants to double check something, taking him to the confirmation page.

问题出在最后一步.确认页面被标记为no-cache,因此必须再次从服务器请求.但是要正确显示相同的页面,第一次传递的相同数据需要再次发送.

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.

这会导致 Joe 被收取两次费用,因为发出的新请求的信息与上次相同.当 Joe 发现他的帐户上有两项费用并且他家门口有一对额外的帐篷时,他将不会是一个快乐的露营者.

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.

这种情况似乎很常见,现在大多数浏览器都是标准错误,而且显然是较新版本的 Android.该错误实际上源自 Chromium,这就是为什么您会在 Google Chrome 中看到相同的错误,以及为什么您只会在 4.4(引入了新版本的 WebView 基于 Chromium).

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).

事实上,您实际上可能之前已经看过它,它是大多数浏览器中显示的消息,警告您要刷新此页面,浏览器将不得不重新发送数据...yada yada yada".

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

这是 Android 4.4 警告您正在发生的事情的方式.如何修复它实际上取决于您连接的对象,但是如果您搜索这种情况,您会发现它很常见,并且有修复方法.错误的确切触发实际上是无法从缓存中处理请求(在这种情况下,no-cache 导致了这种情况).

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).

根据请求的性质,可能实际上不需要 no-cache.

Depending on the nature of the request, maybe no-cache isn't actually needed.

但从您的应用程序的角度来看,主要问题是,onReceiveError 是 WebView 的一种最后手段".你在那里得到的错误是从底层系统传播的.一旦你到达那里,你就不能继续加载页面现在.因此,您没有机会允许重新发送,也不能向用户提供该选项,这与 Google Chrome 的说法不同.

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.

这篇关于Android 4.4 在 onReceivedError 中为 WebView 返回 ERR_CACHE_MISS 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆