2012-08-12 69 views
0

我正在開發一個移動應用程序codenameone如何在java/codename中處理請求超時?

我需要連接到服務器並從服務器獲取一些數據。

當數據被提取時,我希望顯示一個對話框。

但是,我還需要確保應用程序不會一直等待以獲取數據。正如在某些特定的持續時間(例如5秒)中,如果數據沒有被取出,我希望終止與服務器的連接請求,並顯示一條消息,其中說明'數據未被取回!'()。

所以..我目前做的是:

1)declare a timerTask which has the following line: 

    NetworkManager.getInstance().addToQueueAndWait(c); //c is my connection object and i have //implemented the method readResponse() in it. 

2)The method readResponse has a string which stores data fetched from server. this string is global and has a default value of "-1". 

3)schedule a timer to run for every 5 secs with the above timerTask. 

4)On the second run of the timer, I check if my string's value (which stores data fetched from server;) has changed. If it has, i show the string in the dialog box else, I just say that data not fetched. 

5)Cancel the timertask and the timer. 

什麼,我想知道的是: 「我是在做正確嗎?」我如何確保我的連接請求被終止,並且沒有任何流水線正在運行?

有沒有更好的方法來實現我想要實現的目標?

回答

1

爲什麼不使用NetworkManager.setTimeout()

+0

啊..謝謝你.. – Nikhil 2012-08-13 10:39:51