7

I have to load XML data in my app, I'm doing that in a subclass of my activity class extending AsyncTask like this :

public class MyActivity extends Activity {

    ArrayList<Offre> listOffres;

    private class DownloadXML extends AsyncTask<Void, Void,Void>
    {
        protected Void doInBackground(Void... params)
        {
            listOffres = ContainerData.getFeeds();
            return null;
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_liste_offres);

        DownloadXML.execute(); // Problem here !

        for(Offre offre : listOffres) { // etc }
    }
}

I don't know how to use execute() here, I have the following error :

Cannot make a static reference to the non-static method execute(Integer...) from the type AsyncTask

I guess some parameters but what ?

Thank you.

Rob
  • 15,732
  • 22
  • 69
  • 107

6 Answers6

17

You need to create an instance of your DonwloadXML file and call execute() on that method:

DownloadXML task=new DownloadXML();
task.execute();

EDIT: you should probably also return the listOffers from your doInBackground() and process the array in the onPostExecute() method of your AsynTask. You can have a look at this simple AsyncTask tutorial.

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
5

you should call it like:

new DownloadXML().execute();
Nermeen
  • 15,883
  • 5
  • 59
  • 72
1

Actually you are calling the method of AsyncTask (which further extends the AsyncTask class) without making an OBJECT of that class. you can call the execute method in two ways.

  1. make an object/instance of the class like

    DownloadXML task=new DownloadXML();
    task.execute();
    
  2. by using an flying object.

    new DownloadXML().execute();
    

I prefer to use here 2nd method to do that.

Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124
1

Well if you want to execute some code by async task in java you can also do this:

AsyncTask.execute(new Runnable() {
            @Override
            public void run() {

                // add code which you want to run in background thread

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // add code which you want to run in main(UI) thread
                    }
                });
            }
        });

And in kotlin if you are using anko, there is much more simpler way to acheive this:

  doAsync {
            // add code which you want to run in background thread
            uiThread {
                // add code which you want to run in main(UI) thread
            }
        }
Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
  • I want to execute the UI code after the completion of the background work, Is there any callbacks available for that? – MarGin Sep 28 '20 at 16:44
  • @MarGin Check this link: https://www.journaldev.com/9708/android-asynctask-example-tutorial The order of execution of methods is: onPreExecute, doInBackground, onPostExecute. onPreExecute and onPostExecute runs on UI thread and doInBackground runs in bg thread. – Suraj Vaishnav Sep 28 '20 at 16:55
  • @MarGin you should coroutines now if you are using kotlin that's the best – Suraj Vaishnav Sep 28 '20 at 16:57
  • thanks for your quick response. can you please share any coroutine example for my purpose – MarGin Sep 28 '20 at 19:09
  • You can check these references: https://codelabs.developers.google.com/codelabs/kotlin-coroutines/#0 https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb – Suraj Vaishnav Sep 28 '20 at 19:31
0

u can execute ur asynctask either

new DownloadXML().execute();

or

 DownloadXML task=new DownloadXML();
 task.execute();

and one more thing u are getting in array in asynctask than it is good to use postexeceute method for ur for loop iteration

as given below

  protected void onPostExecute(String s) {

    for(Offre offre : listOffres) { // do ur work here after feeling array }    


    }
Khan
  • 7,585
  • 3
  • 27
  • 44
0

You have to create object of DownloadXML class first.

DownloadXML downloadxml= new DownloadXML();
downloadxml.execute();

and return listOffres.

listOffres = ContainerData.getFeeds();
return listOffers;
Punam
  • 25
  • 7