1

I have android app that used webview to display Google App Engine web app. How to overwrite the default HTTP Error 504 Gateway timeout encountered in my app?

HTTP Error 504 Gateway timeout

The server, while acting as a gateway or proxy, did not receive a 
timely response from the upstream server it accessed in attempting 
to complete the request.

I already override onReceivedError which work when no internet connection available and other error.

webView.setWebViewClient(new WebViewClient(){
    ...

    @SuppressLint("DefaultLocale")
    @Override
    public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {

        try {
            String template = streamToString(getAssets().open("html/error.html"));
            String data = template.replaceAll("%DESCRIPTION%", description.toLowerCase())
                    .replaceAll("%WEBSITE_URL%", WEBSITE_URL);

            view.loadDataWithBaseURL("file:///android_asset/html/", data, "text/html", "utf-8", null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

});

onReceivedError can't received HTTP errors only network error? Any workaround? How can android webview intercept HTTP errors?

JR Galia
  • 17,229
  • 19
  • 92
  • 144

1 Answers1

0

Actually your request is getting timed out because of low connectivity or network related reasons; you can show a custom page this way:

  webview.setWebViewClient(new WebViewClient() {
            /* (non-Javadoc)
            * @see android.webkit.WebViewClient#onPageStarted(android.webkit.WebView, java.lang.String, android.graphics.Bitmap)
            */
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                System.out.println("page loading started");
                // TODO Auto-generated method stub
                if(!isNetworkAvailable2())
                {
                    //showInfoMessageDialog("network not available");
                   //load here your custom offline page for handling such errors

                    System.out.println("network not available");
                    return;
                }
                else System.out.println("network available");

                super.onPageStarted(view, url, favicon);

            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                isConnected=isNetworkAvailable2();
                if (isConnected) {
                    // return false to let the WebView handle the URL
                    return false;
                } else {
                    // show the proper "not connected" message
                // view.loadData(offlineMessageHtml, "text/html", "utf-8");
                    // return true if the host application wants to leave the current 
                    // WebView and handle the url itself
                    return true;
                }
            }
            @Override
            public void onReceivedError (WebView view, int errorCode, 
                String description, String failingUrl) {
                if (errorCode == ERROR_TIMEOUT) {
                    view.stopLoading();  // may not be needed
                // view.loadData(timeoutMessageHtml, "text/html", "utf-8");
                }
            }
        });
        //webview.setWebChromeClient(new WebChromeClient());        
    }

Credits to the original answer here.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nezam
  • 4,122
  • 3
  • 32
  • 49