4

Is there a way to set the timeout value in WebView? I want the WebView to be time-outed if the url is too slow to response or if the site doesn't load in 10 seconds.

I don't know really where I should start :(

I have 2 classes, the first one is starter and the second one is for webView. Starter is a kind of thread and splash-screen for start and the next activity is the main webView, so I would like to add a checker if the site doesn't response in 4 10 secs, it would give a error.

I hope any of you can help me,

M. Mariscal
  • 1,226
  • 3
  • 17
  • 46
Tirolel
  • 928
  • 3
  • 17
  • 43

3 Answers3

3

You can do it by setting up a Timer which checks for progress of current page by calling getProgress() and if it is less than some threshold after some specified time then you can dismiss the loading of the current page.

Krishnakant Dalal
  • 3,568
  • 7
  • 34
  • 62
  • Can u help me more with the the solution please? u have some example i can follow? – Tirolel May 04 '12 at 13:59
  • http://stackoverflow.com/a/4303028/609782 . A implementation of the same would help us learn better. Copy paste would not. – Darpan Feb 25 '14 at 06:47
2

Here is the code to implement a timer that will check the progress and triggers action if it takes more than a certain time to load the page.

webView.setWebChromeClient(new WebChromeClient() {
    Long seconds = (long) 0.0;
    public void onProgressChanged(WebView view, int progress) {
        Date interestingDate = new Date();

        // second < 5 here you can use your desired time you wish to wait for.
        while (progress < 100 && seconds < 5) {
            //following line calculates time difference
            //between current time and time when page started loading.
            seconds = ((new Date()).getTime() - interestingDate.getTime())/1000;
        }

        if(progress < 100 && seconds > 5) {
            view.stopLoading();
            Log.d("###", "stopped");
        }       
    }
});
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Darpan
  • 5,623
  • 3
  • 48
  • 80
  • sorry, my edit was hasten and now I cannot roll it back. But tell me please which Seconds you mean and how to get that compiled. – Marvin Emil Brach Nov 06 '12 at 08:36
  • My apologies but I didn't get your question, can you please explain? – Darpan Nov 06 '12 at 13:09
  • You wrote `Seconds=(Long)0;`– I was not aware of that Type, only of the Enum `TimeUnit` so I overhasty edited your post and added that TimeUnit. Then there was the time to paste your code and I got a problem: neither `TimeUnit.Seconds=(Long)0;`nor `Seconds=(Long)0;` is syntactically correct and could become compiled. At least in eclipse Juno with the ordinary ADT libraries and with JDK 1.6. But I'm happy every time I can learn new stuff, so I tried to figure out under which conditions you could compile it if you could at all. Thanks for the time anyway ;) – Marvin Emil Brach Nov 06 '12 at 13:33
1

Darpans code will not work, not at least because the parameter progress will not change during while, only by next call of. Try this instead:

private long starttime = 0;
public void onProgressChanged(WebView view, int progress) {
    Log.v(TAG, "progressChanged: " + progress);
    if(progress == 10) starttime = System.currentTimeMillis();

    long secondsSinceStart = (System.currentTimeMillis() - starttime) / 1000;

    if(progress < 100) {
        Log.v(TAG, "seconds since start: " + secondsSinceStart);
        if(secondsSinceStart > 5){
            view.stopLoading();
            Log.d(TAG, "TIMEOUT -> Stopped.");
        }
    } else {
        Log.v(TAG, progress + "% completed in: " + secondsSinceStart + " seconds");
    }
}

Unfortunately this will only work if the connection to the server will be established. If not the onProgressChanged get called only two times: at 10 and at 100 percents when then load is aborted.

Marvin Emil Brach
  • 3,984
  • 1
  • 32
  • 62