Reputation:
Android.webview.WebViewClient
class lets activities receive an WebViewClient.onReceivedError
callback when the WebView
is unable to load a URL, but there is no callback for HTTP Errors, like: 401, 403, 404, 500. How can we handle this error in WebView
?
Here is some code:
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(R.id.webview1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://192.168.0.6:8081/karbonn/add/apk//3_1/");
// 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("https://www.google.co.in/");
}*/
}
@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("http://trax.claym.in/click.php?c=209&key=c73n783g2tm2377yd73d6n5m");
//webView.loadUrl("https://www.google.co.in/");
//animation url
// webView.loadUrl("http://www.dogwonder.co.uk/wp-content/uploads/2009/12/tumblr_ku2pvuJkJG1qz9qooo1_r1_400.gif");
}
@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);
}
}
Upvotes: 17
Views: 7730
Reputation: 5119
Sorry but you can't.
There you have the bug and thread where the devs are telling so.
https://code.google.com/p/android/issues/detail?id=968
Just you can do on the new Android M (API 23) with the new method
public void onReceivedHttpError (WebView view, WebResourceRequest request, WebResourceResponse errorResponse)
Hope this helps
Upvotes: 5