The key on AsyncTask is that it is "async"hronic. You "launch it" and the
lines of code after that can't be sure it ended.
That's what onPostExecute is for. Google around to see how to send your
result back to your main thread. There are several tutorials about it.

Marina
On Sep 26, 2012 3:51 AM, "Ollie Pennington" <olliepennington1...@gmail.com>
wrote:

> Hey there good forum people, I was hoping you could help me with a really
> frustrating issue I am having...
> Basically, I am using an AsyncTask to run code that connects to a URL,
> which executes a PHP script, and then grabs the output. Within the
> AsyncTask class I can get the information from the URL that I need fine,
> but I cannot seem to move it from the AsyncTask back to my normal code.
> My custom AsyncTask class looks like this:
>
>> private class ScriptExecutionTask extends AsyncTask<URL, Boolean, String>
>>> {
>>
>>
>>
>>         private boolean executionComplete = false;
>>
>>
>>
>>         private String result = null;
>>
>>
>>
>>         @Override
>>
>>         protected void onPreExecute() {
>>
>>             scriptExecutionInProgress = true;
>>
>>         }
>>
>>
>>
>>         @Override
>>
>>         protected String doInBackground(URL... scriptUrl) {
>>
>>             try {
>>
>>                 URL script = scriptUrl[0];
>>
>>                 URLConnection scriptConnection = script.openConnection();
>>
>>                 BufferedReader resultReader = new BufferedReader(new
>>> InputStreamReader(scriptConnection.getInputStream()));
>>
>>                 StringBuilder resultBuilder = new StringBuilder();
>>
>>                 String line;
>>
>>                 while((line = resultReader.readLine()) != null) {
>>
>>                     resultBuilder.append(line);
>>
>>                 }
>>
>>                 resultReader.close();
>>
>>                 executionComplete = true;
>>
>>                 return resultBuilder.toString();
>>
>>             } catch (IOException ex) {
>>
>>                 ex.printStackTrace(System.err);
>>
>>                 return null;
>>
>>             }
>>
>>         }
>>
>>
>>
>>         @Override
>>
>>         protected void onPostExecute(String result) {
>>
>>             this.result = result;
>>
>>             scriptExecutionInProgress = false;
>>
>>         }
>>
>>
>>
>>         protected String getResult() {
>>
>>             return result;
>>
>>         }
>>
>>
>>
>>         protected boolean isExecutionComplete() {
>>
>>             return executionComplete;
>>
>>         }
>>
>>
>>
>>     }
>>
>>
> And this is the code I use to execute the script and then wait for it to
> return the output:
>
>>
>
> // Execute the script
>
>         URL script = new URL(url);
>
>         ScriptExecutionTask scriptExecutionTask = new
>> ScriptExecutionTask();
>
>         scriptExecutionTask.execute(script);
>
>         long timeAtExecute = System.currentTimeMillis();
>
>
>
>         // Wait for script to execute, or time out if it takes too long
>
>         while(!scriptExecutionTask.isExecutionComplete() &&
>> System.currentTimeMillis() - timeAtExecute <= executionTimeOut);
>
>
>
>         latestResult = scriptExecutionTask.getResult();
>
>         return latestResult;
>
>
> The main issue is that 'latestResult' will always be returned as null from
> the above code. When I System.out.println() the result within the AsyncTask
> I get exactly what I wanted. But for some reason, I can't grab the result
> from the AsyncTask and store it in latestResult.
>
> Thank you very much to anyone who can advise me, this has been bothering
> me for a while now :)
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to