How to wait for two Async HTTP request complete with loopj HTTP library

拈花ヽ惹草 提交于 2021-02-08 05:20:24

问题


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;
  }
});
}

回答1:


you could use

SyncHttpClient

instead of

AsyncHttpClient

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

class Example implements AsyncHttpResponseHandler{

    int executedTasks = 2;

    void executeHttpRequests(){
        new HttpExecutor(this).start();
        new HttpExecutor(this).start();
    }

    @Override
    public synchronized void onStart() {
        // called before request is started
    }

    @Override
    public synchronized void onSuccess(int statusCode, Header[] headers, byte[] response) {
        executedTasks--;
        if (executedTasks == 0){
            // continue, otherwise wait for other callback
        }
    }

    @Override
    public synchronized void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
        // called when response HTTP status is "4XX" (eg. 401, 403, 404)
    }

    @Override
    public synchronized void onRetry(int retryNo) {
        // called when request is retried
    }

}

class HttpExecutor extends Thread{

    AsyncHttpResponseHandler callback;

    public HttpExecutor(AsyncHttpResponseHandler callback){
        this.callback = callback;
    }

    @Override
    public void run(){

        SyncHttpClient client = new SyncHttpClient();
        client.get("http://www.google.com", callback);

    }

}


来源:https://stackoverflow.com/questions/27513064/how-to-wait-for-two-async-http-request-complete-with-loopj-http-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!