0

I have an app that uses a web view to display a page, but the web server sometimes goes offline due to various reason I can't control...

When the server crashes, the web view in my app does not open anything and shows only a blank screen... I want in this case, instead of showing a blank screen, that my app could display a message like "The server is offline, please try again later"

How can I do that in Android Studio?

Thanks for the help!

Vladimir
  • 77
  • 2
  • 11

2 Answers2

0

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

0

You can try this:

mWebView.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
         Toast.makeText(getActivity(),  "The server is offline, please try again later" , Toast.LENGTH_LONG).show();

    }
});

you cannot show a message on the webview itself. You can close the webview and display your custom error dialog.

MezzDroid
  • 560
  • 4
  • 11