AsyncTask没有调用onPostExecute

时间:2015-03-23 00:36:05

标签: java android android-asynctask

其他课程:

    SpotifyTask st = new SpotifyTask(new Closure<JSONObject>() {
        @Override
        public void executeOnSuccess(JSONObject result) {
            track.setJson(result);
        }
    });

    st.execute("asd");

成为SpotifyTask:

public class SpotifyTask extends AsyncTask<Object, Void, JSONObject> {

private final Closure<JSONObject> closure;

public SpotifyTask(Closure<JSONObject> closure) {
    this.closure = closure;
}

public static void getTrack(Closure<JSONObject> closure) {
    new SpotifyTask(closure).execute("asd");
}

@Override
protected JSONObject doInBackground(Object... params) {
    JSONObject result = null;
    SpotifyCall spcall = new SpotifyCall();
    try {
        result = spcall.getTrack();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return result;
}

@Override
protected void onPostExecute(JSONObject result) {
    System.out.println("ASD: on post execute "+result);
    closure.executeOnSuccess(result);
}

}

所以... doInBackground运行正常,并且可以正确返回JSONObject;我知道因为我对它进行了解决,“结果”是一个JSONObject。

但onPostExecute永远不会执行,调试器永远不会到达,并且“ASD:on postexecute”+结果永远不会被记录。

有什么建议吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

问题在于我是&#34;持有&#34; UI线程:

    this.status = "loading";
    final Track track = new Track();

    SpotifyTask.getTrack(new Closure<JSONObject>() {
        @Override
        public void executeOnSuccess(JSONObject result) {
            track.setJson(result);
        }
    });

    while (this.status.equals("loading")) {

        if (track.getJson() != null) {
            this.trackUno = track.getJson();
            this.status = "ready";
        } else {
            try {
                System.out.println("Not ready, waiting.");
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

一旦我移除了while块,它就完美无缺。

我必须找到另一种方式来等待&#34;呼叫完成。

感谢您的时间![/ p>