1
votes

am creating a J2ME application which connects/accesses some php Files in a remote server using j2me(HttpConnection). due to some network problems at times the connection block for a long period of time. how would i create a thread to try the connection given a timeout of 10seconds. if the connection doesnt respond within the 10seconds the thread waits for another 5 seconds and retries again. the maximum number of retries should be 3 before the user is alerted that there is not network connection available.

1

1 Answers

0
votes

You can use TimerTask class in either way, to check the Timeout interval of 10 seconds as follows,

// First do your HttpConnection and open your URL
HttpConnection httpConnection = (HttpConnection) Connector.open(URL);

responseCode = httpConnection.getResponseCode(); // responseCode is class variable

// Now create a timertask that invokes after 10 seconds,
Timer timer = new Timer(); 

timer.schedule ( new TimeoutTask(), 10 * 1000 ); 

...
private class TimeOutTask extends TimerTask
{
    public void run()
    {
      // check reponseCode's value here, if it is not 200 then there is problem in network connection.
    }
}