我创建了一个使用j2me(HttpConnection)连接/访问远程服务器中的一些php文件的J2ME应用程序。由于一些网络问题,有时会造成连接阻塞很长一段时间。在10秒超时的情况下,我如何创建一个线程来尝试连接。如果连接在10秒内没有响应,线程将再等待5秒,然后重试。在警告用户没有可用的网络连接之前,最大重试次数应为3次。
发布于 2012-10-17 23:04:10
您可以通过任意一种方式使用TimerTask类,检查10秒的超时间隔,如下所示:
// 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.
}
}https://stackoverflow.com/questions/12932674
复制相似问题