M
Marcelo
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
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