1

我正在尝试使用 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);
    }
}
4

1 回答 1

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);
                     }
                 });
            }
        });        
于 2013-05-04T00:43:20.030 回答