Android WebViewClient onReceivedError is not called for a 404 error

前端 未结 5 2142
不知归路
不知归路 2021-02-19 10:57

hi
In a list view i have an webview which should load a image file from the server,when there is no image present i need a dummy image .I tried

holder.imag         


        
5条回答
  •  眼角桃花
    2021-02-19 11:31

    @Neeraj is on the right track, but my app allows a refresh of the webview, so I need to clear the error state before any new URL load. Furthermore, the error flag must be stored as a data member on the parent activity so that it persists during onPageStart() and onPageFinish()--those methods can be called after 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();
          }
    

提交回复
热议问题