Android webView加载失败显示自己的view或者加载URl (仿微信webView加载进度条)

webView加载失败显示的View

webView.setWebViewClient(new WebViewClient() {
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
        super.onReceivedHttpError(view, request, errorResponse);
        // 这个方法在6.0才出现
        int statusCode = errorResponse.getStatusCode();
        System.out.println("onReceivedHttpError code = " + statusCode);
        if (404 == statusCode || 500 == statusCode) {
            view.loadUrl("about:blank");// 避免出现默认的错误界面
            //view.loadUrl(""); //这里是加载url也可以
            mTitleTv.setText("小欧出了点小问题");
            webView.setVisibility(View.GONE);
            errorView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        // 断网或者网络连接超时
        if (errorCode == ERROR_HOST_LOOKUP) { //无网络加载
            view.loadUrl("about:blank"); // 避免出现默认的错误界面
            //view.loadUrl(""); //这里是加载url也可以
            mTitleTv.setText("网络异常");
            reload.setVisibility(View.GONE);
            webView.setVisibility(View.GONE);
            no_network_view.setVisibility(View.VISIBLE);
        } else if (errorCode == ERROR_CONNECT || errorCode == ERROR_TIMEOUT) {//连接到服务器失败和连接超时
            //view.loadUrl("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3569288516,1357934275&fm=26&gp=0.jpg");
            mTitleTv.setText("小欧出了点小问题");
            webView.setVisibility(View.GONE);
            errorView.setVisibility(View.VISIBLE);
        }
    }
});

 

加载进度条

webView.setWebChromeClient(new WebChromeClient() { //加载进度条
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        Log.e("newProgress", newProgress + "");
        progressBar.setProgress(newProgress);
        if (newProgress >= 100) {
            progressBar.setVisibility(View.GONE);
        }
    }
});

你可能感兴趣的:(Android webView加载失败显示自己的view或者加载URl (仿微信webView加载进度条))