I have written an application that queries a web service I wrote (which returns JSON data). My app currently processes the web service call using an AsyncTask and updates a TableLayout with the data it receives. I want my app to regularly (every second or so) call this web service and display the data in the DB, as it is continuously being updated. How can I go about doing this without having the UI thread block?
Currently the way things work is the user presses a “go” button, and the AsyncTask displays a “loading” dialog while the request processes. I would like for them to press the go button once and have the data refresh in the layout. I’m not sure what the best way to architect this is.
I wouldn’t recommend that you create a new
AsyncTaskevery second since this is going to result in a lot of object creation and corresponding memory collection.What you can do instead is create an
AsyncTaskthat after each request returns from the web service updates some internal data structures and then callspublishProgress(), waits the appropriate amount of time, then makes a new request to the web service. InonPublishProgress()the code should then get the new information from the request from whatever internal structures are being used (don’t forget to use a lock here to synchronize access) and refresh the UI.You’ll also want the
AsyncTaskto have a method or variable that theActivitycan call to tell it to break out of the loop.