a Timer in a URLConnection
- From: Marcelo <mache_1999@xxxxxxxxx>
- Date: Tue, 29 Nov 2005 23:11:26 +0100
Hello everybody,
I would like to implement a timer into a URLConnection object to avoid some server useless conections. My application can connect to the server, but the server doesn't send back any releavant information (a file) but it mantains the connection alive.
I would like to make a Timer in order to stop the connection if there is no response from the server in a given amount of time. I have this kind of code:
//Connect to the server con.connect(); final boolean isGettingData = false;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("Time's up! "+isGettingData);
//Kill the URLConnection
con = null;
}
}, 2000);
//Get the length of the image
//Normally the application stops here if the server is giving useless information
//that mantains the connection opened
int length = con.getContentLength();
//If the application passes here, the server is sending the information that I need //That's why this variable changes. isGettingData = true;
However, this doesn't work fine for many threads that acces this method. Do you have an idea for implementing better this code?
thanks for your help.
Marcelo
.
- Follow-Ups:
- Re: a Timer in a URLConnection
- From: Marcelo
- Re: a Timer in a URLConnection
- From: zero
- Re: a Timer in a URLConnection
- Prev by Date: POST request as image url
- Next by Date: Re: Creating an object during runtime
- Previous by thread: POST request as image url
- Next by thread: Re: a Timer in a URLConnection
- Index(es):
Relevant Pages
|