2012-10-17 28 views
1

正在创建一个J2ME应用程序,它使用j2me(HttpConnection)连接/访问远程服务器中的一些php文件。由于长时间连接阻塞时有些网络问题。我将如何创建一个线程来尝试10秒的超时连接。如果连接在10秒内没有响应,则线程再等待5秒钟并再次重试。在用户被警告没有可用的网络连接之前,最大重试次数应该为3。j2me HttpConnection

回答

0

可以使用的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. 
    } 
} 
+0

J2ME HttpConnection的。使用TimerTask检查超时时间间隔,我将如何确保连接重试次数最多为3次,然后提醒用户连接出现问题并正常退出连接线程。 – glapo

+0

你必须编码你的自我,一旦你失败了,重新执行这个代码尽可能多的时间作为你的要求重新检查。 – Lucifer