2

Fixed: Turns out the problem was that I was clearing the ArrayList after passing it in to the Task, so by the time it got around to executing it, there was nothing there to send. It was working in debug because it could execute before I let it step to the next line. Lesson learned: dont pass in variables that are reused for long running tasks :) Thanks to those of you who answered.

I'm trying to have user selection data sent to my webserver using an async task. Typically the user will make a selection and a string of 3 or 4 commands are sent to an AsyncTask to handle this. When I run the code, the AsyncTask is being created, but never executing the doInBackground method. Even more peculiar is that when I run in debug mode and have it step through the task creation, it works perfectly.

private class SendCommandTask extends AsyncTask< ArrayList<Command>, Void, String>{

        protected String doInBackground(ArrayList<Command>... requests){
            conn = null;
            ArrayList<Command> values = requests[0];

            for( int i = 0; i < values.size(); i++){
                try {
                    url = new URL( siteRoot + values.get(i).getEndPoint() );
                    conn = (HttpsURLConnection) url.openConnection();
                    conn.setRequestProperty("Content-Type", "application/json");
                    conn.setRequestProperty("Cookie", authCookie);
                    conn.setReadTimeout(10000 /* milliseconds */);
                    conn.setConnectTimeout(15000 /* milliseconds */);
                    conn.setDoOutput(true);

                    OutputStreamWriter os = new OutputStreamWriter(conn.getOutputStream());
                    os.write(values.get(i).getJson().toString());
                    os.flush();                     
                }catch (IOException e) {
                    e.printStackTrace();
                }
                finally{
                    conn.disconnect();              
                }
            }
            return null;
        }
    }

which is called from the UI thread via the following method:

public void sendCommand(ArrayList<Command> commands){
        new SendCommandTask().execute(commands);
    }

I've looked at similar threads but they just suggest using a different method, which I would rather not do since this seems to be working (in debug :( )

cmcowart
  • 193
  • 1
  • 13
  • Looks fine (except where is `conn` defined?)... Are you sure it is not executing? AsyncTasks always worked for me. – P Varga Dec 21 '11 at 22:46
  • conn is is declared as a class variable in the class that houses the task, it's just reusing it. Positive it is not executing (my actual code has logcat prints during execution) – cmcowart Dec 21 '11 at 22:55
  • 1
    If you put `Log` calls before calling `execute()` and in `onPreExecute()`, `doInBackground()`, and `onPostExecute()`, then what shows up in logcat? – Steve Blackwell Dec 21 '11 at 22:58
  • The issue was that I was clearing the ArrayList that I passed in in the UI thread before it got a chance to execute in the task. I didnt realize it didnt clone the variable for me. The for loop in doInbackground was never executing because the ArrayList was empty :| – cmcowart Dec 21 '11 at 23:07

1 Answers1

2

If it seems to not be executing, something might be throwing an Exception in doInBackground, in which case the LogCat will have a stacktrace from the background thread (maybe something like siteRoot is null?)

P Varga
  • 19,174
  • 12
  • 70
  • 108
  • not getting anything in LogCat other than what I put in there to try and debug this :( – cmcowart Dec 21 '11 at 23:00
  • What happens when you manually call doInBackground() in the UI thread? – P Varga Dec 21 '11 at 23:03
  • 1
    Problem is that I was emptying the ArrayList that I passed in before it was getting a chance to execute, so the for loop in the doinbackground method was never executing. Didn't realize it didnt clone the variable for me. Thanks anyways! – cmcowart Dec 21 '11 at 23:05
  • 1
    `CopyOnWriteArrayList` is handy for such occasions. – P Varga Dec 22 '11 at 06:14