Other than what Marina said, your code also is critically flawed.
The While loop you've used there basically forces the UI thread to wait for 
the ASyncTask to finish... This nullifies the whole concept of running the 
ASyncTask and you'll still get ANRs from android.

What you should do is split your code. ASyncTasks make it the simplest ever:
you break down your code into 3 parts - What happens before the background, 
what happens during the background and what happens after the background.
The "Before" part goes into "onPreExecute", the background part goes to 
"doInBackground" and the "after" part goes into "onPostExecute"

Look up some tutorials regarding ASyncTasks and how to use them.


On Tuesday, September 25, 2012 7:54:00 PM UTC+2, Ollie Pennington 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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to