timeout for downloading a file
I am downloading a file using this way
URL url = new URL(URL)
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setConnectTimeout(TimeOut);
connection.setReadTimeout(TimeOut);
connection.connect();
String status = connectio开发者_Python百科n.getHeaderField(0);
what I want do is if the file is not downloaded in the specified time then it stops download or give timeout Exception.
I had setConnectTimeout()
but only give exception if the connection is not established
within the initial connection time.
I have not tried this, but I guess you could set a boolean variable to 'finished = false;' before you start downloading. Then set this to 'finished = true;' when download is finished. Then, create a thread that checks this variable on a regular interval or after the desired time. If the boolean value does not have the desired state, abort the download (disconnect connection, close the stream).
Also have a look at the AsyncTask-class.
精彩评论