Android WebViewClient onReceivedError不会被调用为404错误


在列表视图中,我有一个webview应该从服务器加载图像文件,当没有图像存在,我需要一个虚拟的形象。我试过

holder.image.setWebViewClient(new WebViewClient() { @Override public void onReceivedError( WebView view, int errorCode, String description, String failingUrl) { System.out.println("description error" + description); view.setVisibility( View.GONE ); } @Override public void onPageFinished(WebView view, String url) { view.setVisibility( View.VISIBLE ); } }); 

我有一个虚拟的图像在一个FrameLayout的webview,onPageFinished监听器被调用后,每个图像的URL加载,但onReceivedError不会调用一个URL产生一个404错误。任何猜测如何做到这一点。

Solutions Collecting From Web of "Android WebViewClient onReceivedError不会被调用为404错误"

今天我有同样的问题,

问题:onPageFinished总是被调用。 如果有错误,将在onErrorReceived之后调用。

这是我find的解决scheme:

 holder.image.setWebViewClient(new WebViewClient() { private boolean error; @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); error = false; } @Override public void onReceivedError( WebView view, int errorCode, String description, String failingUrl) { error = true; System.out.println("description error" + description); view.setVisibility( View.GONE ); } @Override public void onPageFinished(WebView view, String url) { if (!error) { view.setVisibility( View.VISIBLE ); } error = false; } }); 

我不得不重写WebViewClient.onReceivedHttpError()而不是WebViewClient.onReceivedError()。

  @Override public void onReceivedHttpError(final WebView view, final WebResourceRequest request, WebResourceResponse errorResponse) { final int statusCode; // SDK < 21 does not provide statusCode if (Build.VERSION.SDK_INT < 21) { statusCode = STATUS_CODE_UNKNOWN; } else { statusCode = errorResponse.getStatusCode(); } Log().d(LOG_TAG, "[onReceivedHttpError]" + statusCode); } 

从WebClient文档:

 /** * Notify the host application that an HTTP error has been received from the server while * loading a resource. HTTP errors have status codes &gt;= 400. This callback will be called * for any resource (iframe, image, etc), not just for the main page. Thus, it is recommended to * perform minimum required work in this callback. Note that the content of the server * response may not be provided within the <b>errorResponse</b> parameter. * @param view The WebView that is initiating the callback. * @param request The originating request. * @param errorResponse Information about the error occured. */ public void onReceivedHttpError( WebView view, WebResourceRequest request, WebResourceResponse errorResponse) { } 

这段代码看起来不错。 有没有可能你的网页没有生成404错误?

@Neeraj是正确的轨道上,但我的应用程序允许刷新的Web视图,所以我需要清除错误状态之前加载任何新的URL。 此外,错误标志必须作为数据成员存储在父活动中,以便在onPageStart()和onPageFinish()期间持续存在 – 可以 onError() 之后调用这些方法。

 public class MyActivity extends Activity { private boolean isError; ... protected void onResume() { super.onResume(); isError = false; myWebView.loadUrl(myUrl); } public class MyWebViewClient extends WebViewClient { /** * can be called even after error (embedded images?), so error flag must keep state as data-member in activity, cleared by activity before each loadUrl(); */ @Override public void onPageFinished(WebView view, String url) { if (!isError) showContent(); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { isError = true; showError(); } 
 holder.image.setWebViewClient(new WebViewClient() { boolean bReceivedError = false; @Override public void onReceivedError( WebView view, int errorCode, String description, String failingUrl) { bReceivedError = true; view.setVisibility( View.GONE ); } @Override public void onPageFinished(WebView view, String url) { if(!bReceivedError) view.setVisibility( View.VISIBLE ); } });