[code]WebView webView = new WebView(this);webView.setWebViewClient(new WebViewClient(){ @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); }}); webView.loadUrl("你加载的网址");
[code]@Overridepublic void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) { super.onReceivedHttpError(view, request, errorResponse); }
[code]@SuppressWarnings("deprecation")@Overridepublic void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // Handle the error}@TargetApi(android.os.Build.VERSION_CODES.M)@Overridepublic void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) { // Redirect to deprecated method, so you can use it in all SDK versions onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());}
网上有很多朋友说直接在onReceivedError方法里面可以直接捕获到404,但是我试了一下,依然是不行。如果有朋友试了可行的话,希望能贴出你的代码,分享一下。毕竟6.0系统刚出来不久,市面上也还没有普及,能用老方法解决最好。在这里先表示感谢了。
转自:http://www.th7.cn/Program/Android/201512/736148.shtml