Detecting Webview Error and Show Message
All answer above are deprecated.
You should use this code after on Page finished
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
//Your code to do
Toast.makeText(getActivity(), "Your Internet Connection May not be active Or " + error.getDescription(), Toast.LENGTH_LONG).show();
}
How to handle errors inside webview?
Check as:
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.e("ProcessPayment", "onReceivedError = " + errorCode);
//404 : error code for Page Not found
if(errorCode==404){
// show Alert here for Page Not found
view.loadUrl("file:///android_asset/Page_Not_found.html");
}
else{
}
}
Android app show an error message if web server is offline
You should use this code after on Page finished
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
//Your code to do
Toast.makeText(getActivity(), "Your Internet Connection May not be active Or " + error , Toast.LENGTH_LONG).show();
}
Other method : You can check if the url code return a 200 with a GET request. If it's something else then 200 then the server is down.
You can find more informations here :
https://stackoverflow.com/a/37145639/9005570
How to check if webview failed to load page (android)?
Use a WebClient on your web view as follow :
webView.setWebViewClient(new WebViewClient(){
@Override public void onReceivedError(WebView view, WebResourceRequest request,
WebResourceError error) {
super.onReceivedError(view, request, error);
// Do something
}
});
Swift : how to show error message in webview when there is no internet connection?
You can use the delegate method:
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
print("webview did fail load with error: \(error)")
let message: String = error.localizedDescription
let alert = UIAlertController(title: "something", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default) { action in
// use action here
})
self.present(alert, animated: true)
}
WebView detect when website has loaded successfully
One way to do this: Use this (deprecated but still functional) api. Will be called for the main url you are loading, not every web resource like the newer version. This should take care of both no internet connection and http error responses.
@SuppressWarnings("deprecation")
override fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String) {
//hide it, etc...
// println("ON receive error: $errorCode $description $failingUrl")
}
Related Topics
Android: How to Check How Much Memory Is Remaining
Android Imagegetter Images Overlapping Text
Failed to Find Build Tools Revision 21.1.1 - Sdk Up to Date
Speechrecognizer Causes Anr... I Need Help with Android Speech API
How to Add Cardview in Layout Xml in Androidx
Android: Radio Button in Custom List View
Ble Scan Is Not Working When Screen Is Off on Android 8.1.0
Creating Animation on Imageview While Changing Image Resource
Hide Tablayout on Scroll of Content Instead of Toolbar
How to Create a Persistent Alarmmanager
Handle Screen Orientation Changes When There Are Asynctasks Running
How to Add a View Programmatically to Relativelayout
How to Set a Fragment Tag by Code
Adding a Vertical Scrollbar to an Alertdialog in Android
How to Detect If Changes Were Made in The Preferences