2

我正在将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)。

4

2 回答 2

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;
    }
}
于 2013-05-17T07:33:34.590 回答
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
        }
    }

}
于 2013-05-16T15:31:12.227 回答