我正在使用codenameone
开发移动应用我需要连接到服务器并从服务器获取一些数据。
获取数据时,我希望显示一个对话框。
但是,我还需要确保应用程序不会一直等待获取数据。如果在某些特定的持续时间(例如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.
我想知道的是:“我这样做是否正确?”如何确保我的连接请求被终止并且没有运行杂散线程?
有没有更好的方法来实现我想要实现的目标?
答案 0 :(得分:1)
为什么不使用NetworkManager.setTimeout()
?