android webview catch bad url
I faced the same problem, i solved it like below:
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
*************************************
}
}
}
I added some logic in onPageFinished(String url) method to check whether the url is valid, and you can catch the exception when the URL is not good (a random string, url = "abc").