I am developing a mobile app with codenameone
I need to connect to the server and fetch some data from the server.
When the data is fetched, I wish to show a dialog box.
However, I also need to ensure that the app does not keep waiting forever to fetch the data. As in, if for some particular time duration (say 5sec) the data is not fetched, I wish to kill my connection request to the server and show a message which says ‘data not fetched!’
So.. WHAT I CURRENTLY DO IS:
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.
What i wish to know is: “Am I doing this correctly?” How do I ensure that my connection request is killed and that there are no stray threads running?
Is there a better way to achieve what I am trying to achieve?
Why not use
NetworkManager.setTimeout()?