2012-10-17 30 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