0

我通过包含 AsyncTask 的外部类从服务器检索数据:

public class GetTask extends AsyncTask<String, Void, JSONObject> {

    private Context context;
    ProgressDialog dialog;

    public GetTask(Context cxt) {
        context = cxt;
        dialog = new ProgressDialog(context);
    }

    protected void onPreExecute() {
        dialog.setTitle("Load...");
        dialog.setMessage("Data...");
        dialog.show();

        super.onPreExecute();
    }

    @Override
    protected JSONObject doInBackground(String... url) {

        // code for retreive data
        return jArray;

    }

    protected void onPostExecute(JSONObject object) {
        dialog.dismiss();
        super.onPostExecute(object);
    }
}

我从我的活动中调用此任务:

Tasks task = new Tasks();
JSONObject json = task.new GetTask(this).execute(ServerURL).get();

我的数据检索成功,但在 super.onPostExecute(object); 之后显示 ProgressDialog;方法,为什么?

PS对话框显示后:

    // Make sure the identity of this thread is that of the local process,
    // and keep track of what that identity token actually is.
    Binder.clearCallingIdentity();
    final long ident = Binder.clearCallingIdentity();

在内部 Looper.class

对不起,我的英语不好。))

4

1 回答 1

0

我找到了解决方案,需要使用回调而不是使用 .get() 方法。我称我的任务为:

callTask(linkODeatails, obj);

调用任务:

void callTask(String link, String object){
    task.new GetTask(this).execute(link + object);

}

我创建界面:

public interface AsyncTaskCompleteListener {
public void onTaskComplete(JSONObject result);

}

并添加了我的任务:

        private Activity activity;
    private AsyncTaskCompleteListener callback;

    public GetTask(Activity act){
        this.activity = act;
        this.callback = (AsyncTaskCompleteListener)act;
    }

他们叫:

callback.onTaskComplete(result, object);
于 2013-05-06T05:38:43.437 回答