Come possiamo gestire gli errori HTTP, come: 401, 403, 404, 500 in android.webview?

Android.webview.WebViewClient class Android.webview.WebViewClient consente alle attività di ricevere un callback WebViewClient.onReceivedError quando WebView non è in grado di caricare un URL, ma non vi è alcuna callback per errori HTTP, ad esempio 401, 403, 404, 500. Come possiamo gestire questo errore in WebView ?

Ecco un codice:

 public class WebViewActivity extends Activity { WebView webView; public String TAG="log"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webview); webView = (WebView) findViewById(; webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl(""); // webView.setWebChromeClient(new WebChromeClient()); webView.setWebViewClient(new WebViewClient(){ @Override public void onPageFinished(WebView view, String url) { // TODO Auto-generated method stub super.onPageFinished(view, url); String pageTitle = view.getTitle(); Toast.makeText(getBaseContext(), pageTitle, Toast.LENGTH_SHORT).show(); // view.get /* if (pageTitle.contains("Apache Tomcat/7.0.40 - Error report")) { // view.goBack(); WebViewActivity.this.finish(); //view.loadUrl(""); }*/ } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { //Log.e(Tag, "onReceivedError"); //int i=errorCode; if (errorCode == ERROR_HOST_LOOKUP || errorCode == ERROR_FILE_NOT_FOUND) { // TODO your dialog here Log.e("error", "ERROR_FILE_NOT_FOUND"+errorCode); } Log.e("error", "onReceivedError"+errorCode); if (errorCode==-10) { WebViewActivity.this.finish(); } String errormsg=description; String url=failingUrl; // TODO Auto-generated method stub Log.e("error code:" ,""+errorCode); super.onReceivedError(view, errorCode, description, failingUrl); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public WebResourceResponse shouldInterceptRequest(WebView view, String url) { // TODO Auto-generated method stub Log.e("WebResourceResponse" ,""+url); return super.shouldInterceptRequest(view, url); } @Override public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) { // TODO Auto-generated method stub return super.shouldOverrideKeyEvent(view, event); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub String myurl=url; Log.e("url", ""+myurl); return super.shouldOverrideUrlLoading(view, url); } }); //webView.loadUrl(""); //webView.loadUrl(""); //animation url // webView.loadUrl(""); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // Check if the key event was the Back button and if there's history if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { webView.goBack(); return true; } // TODO Auto-generated method stub return super.onKeyDown(keyCode, event); } }