android webview抓坏url

在我的应用程序中,我从Web服务接收URL作为字符串并将其加载到WebView。

mainContentText = (WebView) findViewById(R.id.mainContentText); mainContentText.getSettings().setJavaScriptEnabled(true); mainContentText.setWebViewClient(new CustomWebClient()); mainContentText.loadUrl(url); private class CustomWebClient extends WebViewClient{ private static final String TAG = "WebWiewActivity"; @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.d(TAG, "loading: " + url); return false; } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); Log.e(TAG, String.format("*ERROR* Code: %d Desc: %s URL: %s", errorCode, description, failingUrl)); } } 

当测试URL不好的情况(随机字符串, url = "abc" )时,我只收到一个默认的错误页面,但onReceivedError没有,而且没有shouldOverrideUrlLoading回调,也没有exception
我怎么能抓住这种情况呢?

我遇到了同样的问题,我解决了它,如下所示:

 private class CustomWebClient extends WebViewClient { private static final String TAG = "WebWiewActivity"; @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.d(TAG, "loading: " + url); return false; } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); ************************************* // do error handling ************************************* } @Override public void onPageFinished(String url) { // url validation if (!Patterns.WEB_URL.matcher(url).matches()) { ************************************* // do error handling ************************************* } } } 

我在onPageFinished(String url)方法中添加了一些逻辑来检查url是否有效,并且当URL不好时可以捕获exception(随机字符串,url =“abc”)。