"无法执行任务:任务已经在运行"即使我创建一个新的请求对象

时间:2014-07-03 13:48:50

标签: android android-asynctask request facebook-fql

我有一个为每个Facebook对象调用getComments方法的循环,如图所示,getComments方法不会从程序中的任何其他地方调用。

JSONArray feedArr = response.getGraphObject().getInnerJSONObject().getJSONArray("data");
for (int i=0;i<feedArr.length();i++) {
    JSONObject obj = feedArr.getJSONObject(i);
    getComments(obj.getString("id"));
}

从我看到的情况来看,我每次都会创建一个新的Request对象,但每次仍然会收到错误"java.lang.IllegalStateException: Cannot execute task: the task is already running."

private void getComments(final String post) {
        String fqlQuery = "{" +
                  "'comments':'SELECT fromid, text, id, likes, time, user_likes FROM comment WHERE post_id=\""
                  + post + "\" LIMIT 2000'," +
                  "'users':'SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT fromid FROM #comments)'" +
                  "}";

        Bundle params = new Bundle();
        params.putString("q", fqlQuery);

        Session session = Session.getActiveSession();
        new Request(session, 
            "/fql", 
            params, 
            HttpMethod.GET, 
            new Request.Callback(){ 
            public void onCompleted(Response response) {
                //..parsing data
            }
        }).executeAsync();
    }
}

是否有这种情况发生的原因以及如何解决?

3 个答案:

答案 0 :(得分:0)

new Request(session, 
            "/fql", 
            params, 
            HttpMethod.GET, 
            new Request.Callback(){ 
            public void onCompleted(Response response) {
                //INVOKE NEXT FROM HERE
            }
        }).executeAsync();

一次只能运行一个这样的实例。第二个实例只能在第一个实例终止后运行。您试图调用10个并行的内容,这导致了错误。

如果您希望为多个实例运行此操作,可以从onCompleted(响应响应)回调执行此操作。

答案 1 :(得分:0)

//第一次CALL

//PUBLIC DATA MAMBER : feedArr & mCount
int mCount = 0;
JSONArray feedArr = response.getGraphObject().getInnerJSONObject().getJSONArray("data");
JSONObject obj = feedArr.getJSONObject(mCount);
getComments(obj.getString("id"));

// RECURSIVE CALL

private void getComments(final String post) {
        String fqlQuery = "{" +
                  "'comments':'SELECT fromid, text, id, likes, time, user_likes FROM comment WHERE post_id=\""
                  + post + "\" LIMIT 2000'," +
                  "'users':'SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT fromid FROM #comments)'" +
                  "}";

        Bundle params = new Bundle();
        params.putString("q", fqlQuery);

        Session session = Session.getActiveSession();
        new Request(session, 
            "/fql", 
            params, 
            HttpMethod.GET, 
            new Request.Callback(){ 
            public void onCompleted(Response response) 
            {
                 mCount++;
                 if(mCount< feedArr .size())// or feedArr.length()
                 {
                 JSONObject obj = feedArr.getJSONObject(mCount);
                 getComments(obj.getString("id"));
                 }
            }
        }).executeAsync();
    }
}

答案 2 :(得分:-1)

我遇到了类似的问题,并且由于您多次执行.executeAsync();而引发异常。这是因为递归调用将再次执行相同的方法。

一个简单的解决方案是在AsyncTask中执行方法getComments(final String post),然后按.executeAsync()更改.executeAndWait(),这样应用程序将等待每次调用{{1}的结果}。