LoopJ AndroidAsyncHttp -更多的任务



我想在一个活动中使用AsyncHttpClient进行更多任务(我需要通过REST从不同的URL获取和更新数据)。但我不知道该怎么处理。我的想法如下:

private class Synchronize extends AsyncTask<String, String, Boolean> {
    @Override
    protected void onPreExecute() {
        prgDialog.show();
    }
    protected Boolean doInBackground(String... params) {
        url = "http://someurl";
        AsyncHttpClient client = new AsyncHttpClient();
            client.post(getApplicationContext(), url, entity, "application/json", new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(String content) {
                    // do something
                }
                @Override
                public void onFailure(int code, Throwable error, String content) {
                    // do something
                }
            }); 
        url = "http//otherurl";
        client.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
                // do something
            }
            @Override
            public void onFailure(int code, Throwable error, String content) {
                // do something
            }
        });
        return true;
    }
    @Override
    protected void onPostExecute(Boolean b) {
        super.onPostExecute(b);
        prgDialog.dismiss();
        // refresh view
    }
}

但是我不确定我是否能这样做。谢谢你的建议。

如果你已经使用AsyncHttp库,你可能不一定需要使用AsyncTask。我在过去的两个项目中所做的是从ResponceHandler类扩展并使用回调初始化它。你可以让所有的请求从ui线程,如果他们不依赖于彼此和处理回调ui的东西或从回调,但它应该是同步的。下面是一些例子:

public class TrainersJsonHandler extends BaseJsonHttpResponseHandler {
    private OnGetTrainers mCallback;
    private Context mContext;
    private ObjectMapper mMapper;
    public TrainersJsonHandler(OnGetTrainers callback, Context context,
            ObjectMapper mapper) {
        mCallback = callback;
        mContext = context;
        mMapper = mapper;
    }
    @Override
    public void onSuccess(int statusCode, Header[] headers, String rawResponse,
            Object response) {
    }
    @Override
    public void onFailure(int statusCode, Header[] headers, Throwable e, String rawData,
            Object errorResponse) {
        RestUtil.processFail(statusCode, headers, e, mContext, mCallback);
    }
    @Override
    protected Object parseResponse(String responseBody) throws JSONException {
        List<Trainer> result = parseTrainers(responseBody);
        if (result != null && mCallback != null) {
            mCallback.onFinishGetTrainers(result);
        }
        return null;
    }

    public List<Trainer> parseTrainers(String responseBody) {...}
}

和在活动中我实现接口,并做了这样的事情:

    @Override
            public void onFinishGetTrainers(final List<Trainer> trainers) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //do ui staff
                       //you can make here new request or refresh counter but should be synchronize
                    }
                });
            }

希望有帮助。

相关内容

  • 没有找到相关文章

最新更新