0

I'm trying to see how works an Asynctask class in android. In particular i want reveal in real time the status of the class for see when it is running and when it has finished. For do this, i have created a class that extend the main activity and another class that is the asynctaks class. This is my main class:

public class PhotoManagement extends Activity{
private String numberOfSelectedPhotos;
private Bitmap currentImage;
private String initConfiguration = "http://www.something.com";
private String response;
private ArrayList<String> formatPhotoList = new ArrayList<String>(); //create a list that will contains the available format of the photos downloaded from the server
private ArrayList<String> pricePhotoList = new ArrayList<String>(); //create a list that will contains the available price for each format of the photos
DownloadWebPageTask webPage = new DownloadWebPageTask();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
protected void onResume(){
    super.onResume();
    webPage.execute(initConfiguration);
    if(webPage.getStatus() == AsyncTask.Status.PENDING){   
        Log.i("STATUS","PENDING");
    }
    if(webPage.getStatus() == AsyncTask.Status.RUNNING){
        Log.i("","RUNNING");
    }
    if(webPage.getStatus() == AsyncTask.Status.FINISHED){
        Log.i("","FINISHED");
    }
}
}

As you can see i want only see the passages of the status with a simple log. And here there is the asynctask class.

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient(); //create a new http client
            HttpGet httpGet = new HttpGet(url); //create a new http request passing a valid url
            try {
                HttpResponse execute = client.execute(httpGet); //try to execute the http get request
                InputStream content = execute.getEntity().getContent(); //prepare the input stream to read the bytes of the request
                BufferedReader buffer = new BufferedReader(
                        new InputStreamReader(content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s; //until is present a line to read, the response variable store the value of the lines
                }

            } catch (Exception e) {
                Log.i("MyApp", "Download Exception : " + e.toString()); //Print the error if something goes wrong
            }
        }
        return response; //return the response
    }

    @Override
    protected void onPostExecute(String result) {
        result = doInBackground(initConfiguration); //take the result from the DownloadWebPageTask class
        result = result.replace("null", "");
        Log.i("RESULT",""+result);
        //find the price and format value from the result using XmlPullParser
        try {
           XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
           factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            xpp.setInput( new StringReader ( result ) );
            int attributeNumber = xpp.getAttributeCount();
            int eventType = xpp.getEventType();
            String currentTag = null;
            while(eventType != XmlPullParser.END_DOCUMENT){
                if(eventType == XmlPullParser.START_TAG) {
                    currentTag = xpp.getName();
                    if (currentTag.equals("product")){
                        xpp.getAttributeValue(null, "name");
                        formatPhotoList.add(xpp.getAttributeValue(null, "name"));
                        Log.i("FORMAT PHOTO",""+xpp.getAttributeValue(null, "name"));
                    }
                }
                eventType = xpp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
            Log.i("","ERROR XML PULL PARSER");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.i("","ERROR IOEXCEPTION");
        }
    }

}   
}

As you can see i have implemented also the method onPostExecute that should be called when the asynctask method has finished to execute the instructions right? So at this point i don't understand why my log RUNNING and my log FINISHED never appear on the log cat. What i'm doing wrong? I'm tried to follow this topic Android, AsyncTask, check status? but in my case it isn't working.

Thanks

Community
  • 1
  • 1
Hieicker
  • 595
  • 1
  • 11
  • 29

4 Answers4

4

Problem :

You are creating object like

DownloadWebPageTask webPage = new DownloadWebPageTask();

But you are calling asynctask on different object,

new DownloadWebPageTask().execute(initConfiguration);

Solution : It should be like

webPage.execute(initConfiguration);
Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
  • thanks. As you can see i have updated my code. Now it log correctly the running status, but it doesn't log the finished status. Shouldn't he return the finished status automatically? – Hieicker Oct 03 '13 at 10:51
  • result = doInBackground(initConfiguration); what is this line doing? – Ritesh Gune Oct 03 '13 at 11:04
  • this line assign the result of the doInBackground method to the variable "result". In fact after as you can see, i work with this variable. – Hieicker Oct 03 '13 at 11:12
  • no need to use that, you are already getting it in protected void onPostExecute(String result) { – Ritesh Gune Oct 03 '13 at 11:19
  • you are right thanks. Any idea of why the method never show the "FINISHED" log? I have tested it now but nothing change... – Hieicker Oct 03 '13 at 11:27
1
@Override
protected void onResume(){
    super.onResume();
    new DownloadWebPageTask().execute(initConfiguration);

here do like this

@Override
protected void onResume(){
    super.onResume();
    webPage.execute(initConfiguration);
DropAndTrap
  • 1,538
  • 16
  • 24
  • thanks for suggestion. I have updated my code. Now it enter correctly on the running status, but it doesn't show the finished status. Shouldn't he return the finished status automatically? – Hieicker Oct 03 '13 at 10:49
1

You didn't implement webPage.execute(), add it

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • yes thanks. I have fixed my code now, but i'm not receiving the status "finished" in any way. Now i can see correctly the "running" status, so my problem isn't fixed yet thanks. – Hieicker Oct 03 '13 at 13:14
0

Most probably the task hasn't finished or even started yet. As you probably know the AsyncTask is doing it's (background) work on a different thread, so your onResume is running in parallel with it. You can either use the task's get() method to wait for it to finish and get the result of the doInBackground() method and then query for it's status or notify your activity from the task's onPostExecute() method to let it know (and log) that it has finished. I don't recommend you the first option because it will actually block the UI thread and will make your usage of AsyncTask pointless.

stan0
  • 11,549
  • 6
  • 42
  • 59