4

How can I change webview timeout? Now I'm using dirty code like this, but I think that it's not a good solution. Somewhere must be a proper mechanism of setting page loading timeout.

public class HollyDollyWebViewClient extends WebViewClient {
    private static final long TIMEOUT = 30000L;

    private Handler timeoutHandler = new Handler();
    
    private Runnable timeoutRunnable = new Runnable() {
        @Override
        public void run() {
            //show some error message here
        }
    };

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        timeoutHandler.postDelayed(timeoutRunnable, TIMEOUT);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        timeoutHandler.removeCallbacks(timeoutRunnable);
    }
}

UPDATE I wanna have something like this:

public class HollyDollyWebViewClient extends WebViewClient {
    private static final long TIMEOUT = 30000L;

    public HollyDollyWebViewClient() {
        // set timeout value somehow
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        switch (errorCode) {
        case ERROR_TIMEOUT:
            // show error message here
            break;
        }
    }
}
Kurt
  • 678
  • 1
  • 7
  • 24
Dmitry
  • 670
  • 1
  • 6
  • 11
  • Did you try this post http://stackoverflow.com/questions/7772409/set-loadurltimeoutvalue-on-webview – Giru Bhai May 26 '14 at 13:00
  • @GiruBhai It`s something that I`m doing, but insteed thread I`m using handler. I`m lookig for something like: HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, TIMEOUT); webView.setParams(params); The goal is to override default timeout value. – Dmitry May 26 '14 at 13:09
  • any solution here? – nutella_eater Mar 15 '17 at 14:00
  • @alexeypolusovm, I've ended with the following solution: https://gist.github.com/dmba/14edf2486c224ff467d10bc295835fca#file-timeoutwebviewclient-java – Dmitry Mar 16 '17 at 23:02

0 Answers0