Search code examples
androidhttp-redirecterror-handlingwebviewconnection-timeout

Can't redirect failed post requests to my error page in Android WebView


i have this

final String linkErrorPage = "file:///android_asset/ErrorPage.html";

public class Callback extends WebViewClient{
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
        webView.loadUrl(linkErrorPage);
    }}

and this in my mainactivity.java

public class browser extends WebViewClient {
        
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
            webView.loadUrl(linkErrorPage);
        }
    }

when i turn my server off i'm expecting to see my error page from the local assets... if it's a get request everything is fine, i'm seeing the error page... but if it's a post request i'm seeing this page instead: enter image description here

how can i redirect to the error page when a post request fails?


Solution

  • okay i found the solution!

        webView.setWebViewClient(new browser(){
            public void onPageFinished(WebView view, String url) {
    
                String string = "net::ERR_CONNECTION_TIMED_OUT";
                webView.findAllAsync(string);
                webView.setFindListener((activeMatchOrdinal, numberOfMatches, isDoneCounting) -> {
                if(numberOfMatches>0) {
                    webView.loadUrl(linkErrorPage);
                }
    
                });
            }
        });
    

    though it briefly displays the default error page. if there's a better way i'd like to know it... this is not a perfect solution.