如何在java/codename one中处理请求超时?

如何在java/codename one中处理请求超时?,java,codenameone,Java,Codenameone,我正在开发一个带有 我需要连接到服务器并从服务器获取一些数据 提取数据时,我希望显示一个对话框 但是,我还需要确保应用程序不会一直等待获取数据。如中所示,如果在某个特定的时间段(例如5秒)内数据未被提取,我希望终止对服务器的连接请求,并显示一条消息,其中显示“数据未提取!” 所以。。我现在做的是: 1)declare a timerTask which has the following line: NetworkManager.getInstance().addToQueueAndWai

我正在开发一个带有

我需要连接到服务器并从服务器获取一些数据

提取数据时,我希望显示一个对话框

但是,我还需要确保应用程序不会一直等待获取数据。如中所示,如果在某个特定的时间段(例如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.
我想知道的是:“我这样做正确吗?”我如何确保我的连接请求被终止,并且没有杂散线程运行


有没有更好的方法来实现我想要实现的目标?

为什么不使用
NetworkManager.setTimeout()