My case is that I need to get two sets of data from sever with two HTTP get request, then process them together.
I'm using loopj's http library to do the task.
The problem is that since loopj's http is Async, which means I can't determine when they will be complete and which will complete first.They all have their own onSuccess function. I tried to put a boolean flag into each of them, and make the calling activity check the change of AND operation of these two boolean, but seems if I make the main thread wait, the onSuccess Function will also wait...
Is there a way I could determine when the two request are both completed, and then I could proceed with next step?
Code is like:
In Calling Activity:
boolean A;
boolean B;
HttpRequestA();
HttpRequestB();
while (!(A&&B)) {
Thread.sleep(100);
}
HttpRequestA is:
public void HttpRequestA() {
A = false;
HTTPService.get(url, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] header, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] header, String responseString) {
A = true;
}
});
}
you could use
instead of
Then you execute each SyncHttpClient in two different threads and pass the same callback for each SyncHttpClient. Continue with your code when the callback has been executed for the second time. The callback method should be defined with the synchronized keyword to avoid that the function is accessed/invoked by two different threads at the same time.
Edit:
so something like this