如何等待两个带有loopj HTTP库的异步HTTP请求完成 [英] How to wait for two Async HTTP request complete with loopj HTTP library

查看:74
本文介绍了如何等待两个带有loopj HTTP库的异步HTTP请求完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况是,我需要通过两个HTTP get请求从服务器获取两组数据,然后将它们一起处理.

My case is that I need to get two sets of data from sever with two HTTP get request, then process them together.

我正在使用loopj的http库来完成任务.

I'm using loopj's http library to do the task.

问题在于,由于loopj的http是异步的,这意味着我无法确定何时完成和先完成,它们都有自己的onSuccess函数.我试图在每个变量中放置一个布尔标志,并使调用活动检查这两个布尔的AND操作的变化,但是似乎如果我让主线程等待,onSuccess函数也将等待...

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?

代码类似于:

在通话活动中:

boolean A;
boolean B;

HttpRequestA();
HttpRequestB();

while (!(A&&B)) {
  Thread.sleep(100);
}

HttpRequestA是:

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

推荐答案

您可以使用

SyncHttpClient

代替

AsyncHttpClient

然后,您在两个不同的线程中执行每个SyncHttpClient,并为每个SyncHttpClient传递相同的回调.第二次执行回调时,请继续执行您的代码.回调方法应使用synced关键字定义,以避免该功能同时被两个不同的线程访问/调用.

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.

类似这样的

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

    }

}

这篇关于如何等待两个带有loopj HTTP库的异步HTTP请求完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆