如何在Android中使用Loopj AsyncHttpLibrary同时发出两个Http请求?

时间:2013-05-16 12:58:14

标签: java android httprequest loopj

我正在使用Loopj AsyncHttpLibrary作为Android应用程序。我的目的是显示一个包含一些问题和相应答案的页面。我编写的Rest API在不同的端点提供问题和答案:

http://example.com/docs/{id}/questions
http://example.com/docs/{id}/answers

我现在正在做的是为“问题”做一个异步的http请求,然后在第一个请求的成功回调中,我做第二个获取“答案”的请求。这是我清理过的代码:

MyApiClient.getQuestions(docId, new JsonHttpResponseHandler() {
    @Override
    public void onSuccess(JSONObject questionsResponse) {

        MyApiClient.getAnswers(docId, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(JSONObject answersResponse) {

                .... Do stuff ....

            }
        });
    }
});

其中MyApiClient是AsyncHttpLibrary的静态包装器,如库文档中所建议的那样,它看起来像这样:

public class MyApiClient {
    private static final String BASE_URL = "http://example.com/";

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void getQuestions(long docId, AsyncHttpResponseHandler responseHandler) {
        get("docs/" + String.valueOf(docId) + "/questions" , null, responseHandler);
    }

    public static void getAnswers(long docId, AsyncHttpResponseHandler responseHandler){
        get("docs/" + String.valueOf(docId) + "/answers" , null, responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }
}

有没有办法在没有人等待另一个请求的情况下启动这两个请求? 我还需要能够在相同的上下文中使用两个http结果(填充ui)。

2 个答案:

答案 0 :(得分:4)

经过一番挣扎,我发现在我的活动类中保持网络操作状态并不困难。我最终使用了两个布尔变量:“fetchedQuestions”和“fetchedAnswers”,在初始化网络请求之前将其重置为“false”。两个请求都是同时发送的,并且在他们的成功方法中,我检查其他请求是否已完成。

这是代码(使用Loopj AsyncHttpClient库)

public class MyActivity extends Activity {

    private boolean fetchedQuestions;
    private boolean fetchedAnswers;

    private JSONObject questions;
    private JSONObject answers;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        refresh();
    }

    private void refresh(){

        fetchedQuestions = false;
        fetchedAnswers = false;

        long docId = 1;

        MyApiClient.getQuestions(docId, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(JSONObject questionsResponse) {
                questions = questionsResponse;  
                fetchedQuestions = true;
                if (fetchedAnswers)
                    populatePage();

            }
        });

        MyApiClient.getAnswers(docId, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(JSONObject answersResponse) {
                answers = answersResponse;  
                fetchedAnswers = true;
                if (fetchedQuestions)
                    populatePage();

            }
        });
    }

    private void populatePage(){
        // do stuff with "questions" and "answers"
    }     

}

其中MyApiClient定义为

public class MyApiClient {
    private static final String BASE_URL = "http://example.com/";

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void getQuestions(long docId, AsyncHttpResponseHandler responseHandler) {
        get("docs/" + String.valueOf(docId) + "/questions" , null, responseHandler);
    }

    public static void getAnswers(long docId, AsyncHttpResponseHandler responseHandler){
        get("docs/" + String.valueOf(docId) + "/answers" , null, responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }
}

答案 1 :(得分:1)

希望这会有所帮助:

public class SampleActivity extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new GetQuestionsThread().execute();
        new GetAnswersThread().execute();
    }

    public class GetQuestionsThread extends AsyncTask<Void, Void, String> {
        public GetQuestionsThread() {

        }

        @Override
        protected String doInBackground(Void... params) {

            //make post

            return request;
        }

        @Override
        protected void onPostExecute(LString result) {
            // do something with result
        }
    }

    public class GetAnswersThread extends AsyncTask<Void, Void, String> {
        public GetAnswersThread() {

        }

        @Override
        protected String doInBackground(Void... params) {
            //make post
            return request;
        }

        @Override
        protected void onPostExecute(LString result) {
            // do something with result
        }
    }

}