Android WebViewClient onReceivedError is not called for a 404 error

前端 未结 5 2184
不知归路
不知归路 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条回答
  •  旧时难觅i
    2021-02-19 11:28

    I had the same issue today,

    The problem: onPageFinished is called always. If there is an error it will be called after onErrorReceived.

    This is the solution I've found:

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

提交回复
热议问题