"Declare a Boolean public variable to indicate whether the current httpconnection receives a server response.
In your connection line, the variable is set to false before the connection;
Start another monitoring thread, get the connection object of the httpconnection, and cyclically monitor this Boolean public variable. If your Boolean public variable is still false within the specified time (20 seconds later), you can set httpconnection to null. In this way, the connection line will throw an exception and exit. "
I wrote the Timer class for implementation. (I learned how to write on a foreign website)
Class timer extends thread {
/** How many milliseconds each time */
Protected int m_rate = 100;
/** Timeout duration in milliseconds */
Private int m_length;
/** Running time */
Private int m_elapsed;
/**
* Constructor
*
* @ Param Length
* Length of time before timeout occurs
*/
Public timer (INT length ){
// Assign to member variable
M_length = length;
// Set time elapsed
M_elapsed = 0;
}
/**
* Timing again
*
*/
Public synchronized void reset (){
M_elapsed = 0;
System. Out. println ("reset timer ");
}
/**
* If this parameter is set to time-out, you can call this method directly when the server returns an error, which is treated as time-out.
*
*/
Public synchronized void setTimeout ()
{
M_elapsed = m_length + 1;
}
/**
*/
Public void run (){
// Loop
System. Out. println ("timer running ");
For (;;){
// Put the timer to sleep
Try {
Thread. Sleep (m_rate );
} Catch (interruptedexception IOE ){
Continue;
}
Synchronized (this ){
// Increment time remaining
M_elapsed + = m_rate;
// Check to see if the time has been exceeded
If (m_elapsed> m_length &&! Isconnactive) {// isconnactive is a global variable.
// Trigger a timeout
Timeout ();
Break;
}
}
}
}
/**
* Processing upon timeout
*
*/
Public void timeout (){
Httpconnection = NULL;
System. Out. println ("conn time>" + time_out + "Ms ");
}
}