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