What is the best approach to request multiple RESTful calls in one Activity
I have an activity that has multiple spinners. Two of spinners values/data need to be populated from data I retrieve from the web via a RESTful service. The data is in JSON format and pretty basic.
I have thought about using an AsyncTask and in the doInBackground doing two web service calls that will populate two different Ha开发者_StackOverflow中文版shMaps but I'm not sure that this is the best approach in regards to designing for performance.
Is there another way to approach this situation? Maybe have a service that fetches the data when the application starts and cache it locally?
Your suggestions would be appreciated.
You probably would want to use two different AsyncTask
instances so that the requests run in parallel.
If your data should be persisted you could consider saving it to a sqlite database or storing the json String
in SharedPreferences
. Using a Service
sounds fine as well.
Also if you just want to cache it locally for the duration of the current running program (and don't need to worry about persisting it when the program closes), you could create a simple global singleton cache that allows synchronized access to your data.
As far as actually getting the data itself, AsyncTask
is probably the best way (if it's not already cached). Perform the fetch in the doInBackground()
method and do the parsing/storing in the onPostExecute()
method.
This article can help you a lot:
http://neilgoodman.net/2011/12/26/modern-techniques-for-implementing-rest-clients-on-android-4-0-and-below-part-1/
精彩评论