0

I have a loop that calls the getComments method for each Facebook object as shown, the getComments method is not called from anywhere else in the program.

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

From what I see, I am making a new Request object each time, but I still get the error "java.lang.IllegalStateException: Cannot execute task: the task is already running." each time.

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();
    }
}

Is there a reason why this happens and how can I fix it?

octopish
  • 59
  • 2
  • 8
  • possible duplicate of [facebook, android java.lang.IllegalStateException: Cannot execute task: the task is already running](http://stackoverflow.com/questions/15556015/facebook-android-java-lang-illegalstateexception-cannot-execute-task-the-task) – rds Sep 23 '14 at 08:58

3 Answers3

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

Only one instance of this can run at a time. The second instance can run only after the first has terminated. You were trying to invoke 10 parallel intances, which was causing the error.

If you wish to run this for multiple instances, you can do it from onCompleted(Response response) call back.

Parth Kapoor
  • 1,494
  • 12
  • 23
0

//First 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();
    }
}
Parth Kapoor
  • 1,494
  • 12
  • 23
  • Sorry it still doesn't work. I tried debugging it and it turns out I get the error even at the FIRST instance of running the request. – octopish Jul 03 '14 at 15:25
  • the same "java.lang.IllegalStateException: Cannot execute task: the task is already running." error. Even when I set it to loop once. – octopish Jul 04 '14 at 08:27
-1

I had a similar problem, and the exception is been thrown because you're executing .executeAsync(); several times. That happens because recursive calls will execute the same method again.

A simple solution is to execute your method getComments(final String post) inside an AsyncTask and then change .executeAsync() by .executeAndWait(), so the application will wait for the results for each call to getComments(...).

shimatai
  • 1,759
  • 16
  • 18