I have an app, which uses several HTTPRequests
for example
get a session id
get some locationdata
get existing categories
(...) and some more
I created a HTTPRequestHandler
, which basically manages all the AsynTasks
for each Request... This works well, but my problem is, I don't know a good way for managing the different AsynTasks
. For example, you need to get the SessionId Task
before you can start the GetSomeLocationData Task
So in my HTTPRequestHandler
I have a queue, which starts the depending AsyncTasks
like:
private void startSessionIdTask(...) {
//...
GetSessionIdTask mGetSessionIdTask = new GetSessionIdTask(this);
mGetSessionIdTask.execute(url);
}
//and from the postExecute() in GetSessionIdTask I call
public void setSessionId(int mSessionId) {
mDataHelper.setmSessionId(mSessionId); //set id
String url = API_URL + API_GET_FAVORITES + URL_VARIABLE;
List<NameValuePair> params = new LinkedList<NameValuePair>();
params.add(new BasicNameValuePair("session_id", getSessionId()));
String paramString = URLEncodedUtils.format(params, "utf-8");
url += paramString;
//and finally start another Tasks (and so one...)
GetLocationsTask mGetLocationsTask = new GetLocationsTask(this);
mGetSessionIdTask.execute(url);
}
However, this works fine, but the problem is, that (depending on the connection), this queue takes time, and the user can start other AsynTasks
which fail, because some initially data is not loaded yet.
I could set some Boolean like isSessionIdLoaded
or could block the UI for the user, but I'm wondering, if there s any better solution?!
So my question is: Is there a way to put asyntasks in some kind of queue (ArrayList, Map..) which will be executed in a row?