Android - 如何容纳在AsyncTask doInBackground方法中执行的线程

时间:2013-05-04 00:27:08

标签: android android-asynctask loopj

我正在尝试使用loopJ查询Web服务,在此操作期间,我想向用户显示进度对话框。操作完成后,我希望进度对话框解除并启动新的活动意图。

我知道AsyncTask是要走的路。在onPreExecute方法中,我显示了进度对话框。在doInBackground上我正在执行网络操作。而onPostExecute我正在解雇对话框并开始一个新的活动意图。

我的问题是doInBackground将异步执行loopJ网络调用,因此onPostExecute将在我的网络操作之前​​完成。如果您查看我的日志,它将显示:

“开始新活动!” “获取类别服务!”

而不是

“提取类别服务!” “开始新活动!”

如何容纳运行doInBackground的异步任务?在onPostExecute中有没有办法等到我的异步loopJ操作完成?

public class FetchCategoriesServices extends AsyncTask<HITECategory, String, String>
{
    private Category userSelectedCategory;
    private ProgressDialog busyDialog;

    @Override
    protected void onPreExecute()
    {
        busyDialog = ProgressDialog.show(SearchActivity.this, getApplicationContext().getString(R.string.progressDialogTitle), 
                                         getApplicationContext().getString(R.string.progressDialogMessage));
    }

    @Override
    protected String doInBackground(HITECategory... params) 
    {
        userSelectedCategory = params[0];

        String requestCategoryServiceURL = BASE_URL + "GetServices?CategoryID=" + userSelectedCategory.categoryID + "&ResponseType=JSON";

        try 
        {
            Client.get(requestCategoryServiceURL, new AsyncHttpResponseHandler()
            {
                @Override
                public void onSuccess(String jsonResponse)
                {
                    Gson gson = new Gson();
                    CategoryServicesListResponse Response = gson.fromJson(jsonResponse, CategoryServicesListResponse.class);

                    categoryServiceresults = Response.categoryServices;

                    Log.d(getString(R.string.DebugKey), "Fetched category services!");
                }
            });
        } 
        catch (Exception e) 
        {
            Log.d(getString(R.string.DebugKey), "Error connecting to service and fetching category services list");
        }

        return null;
    }

    @Override
    protected void onPostExecute(String params)
    {
        busyDialog.dismiss();
        Log.d(getString(R.string.DebugKey), "Starting new activity!");
        Intent intent = new Intent(getApplicationContext(), CategoriesSearchActivity.class);
        startActivity(intent);
    }
}

1 个答案:

答案 0 :(得分:2)

只需将onPostExecute中的代码放入onSuccess方法:

        Client.get(requestCategoryServiceURL, new AsyncHttpResponseHandler()
        {
            @Override
            public void onSuccess(String jsonResponse)
            {
                Gson gson = new Gson();
                CategoryServicesListResponse Response = gson.fromJson(jsonResponse, CategoryServicesListResponse.class);

                categoryServiceresults = Response.categoryServices;

                Log.d(getString(R.string.DebugKey), "Fetched category services!");
                youractivity.this.runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                           busyDialog.dismiss();
                           Log.d(getString(R.string.DebugKey), 
                                            "Starting new activity!");
                           Intent intent = new Intent(getApplicationContext(),
                                CategoriesSearchActivity.class);
                           youractivity.this.startActivity(intent);
                     }
                 });
            }
        });