Android:如何执行AsyncTask?

7

我需要在我的应用中加载XML数据,我正在通过扩展AsyncTask并将其作为Activity类的子类来实现:

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 }
    }
}

我不知道如何在这里使用execute(),我遇到了以下错误:

无法对AsyncTask的非静态方法execute(Integer...)进行静态引用

我猜测需要一些参数,但是是什么呢?

谢谢。

6个回答

17

您需要创建DownloadXML文件的实例,并在该方法上调用execute()

DownloadXML task=new DownloadXML();
task.execute();
EDIT: 你可能还应该从你的doInBackground()方法中返回listOffers,并在AsynTaskonPostExecute()方法中处理数组。您可以查看这个简单的AsyncTask教程

5

你应该这样调用:

new DownloadXML().execute();

1

实际上,您正在调用AsyncTask的方法(它进一步扩展了AsyncTask类),而没有创建该类的对象。 您可以以两种方式调用execute方法。

  1. 创建一个类的实例对象,如下所示:

    DownloadXML task=new DownloadXML();
    task.execute();
    
  2. 使用匿名对象:

    new DownloadXML().execute();
    

我更喜欢使用第二种方法来做这件事。


1
如果您想在Java中通过异步任务执行一些代码,也可以这样做:
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
                    }
                });
            }
        });

在使用 ankokotlin 中,实现这一点的方法更加简单:
  doAsync {
            // add code which you want to run in background thread
            uiThread {
                // add code which you want to run in main(UI) thread
            }
        }

我想在后台工作完成后执行UI代码,是否有可用的回调函数? - MarGin
@MarGin 请查看此链接:https://www.journaldev.com/9708/android-asynctask-example-tutorial 方法的执行顺序是:onPreExecute、doInBackground、onPostExecute。onPreExecute 和 onPostExecute 运行在 UI 线程上,而 doInBackground 在后台线程上运行。 - Suraj Vaishnav
@MarGin,如果你正在使用 Kotlin,那么现在应该使用协程,这是最好的选择。 - Suraj Vaishnav
谢谢您的快速回复。您能否分享任何协程示例以满足我的需求? - MarGin
您可以查看以下参考资料: https://codelabs.developers.google.com/codelabs/kotlin-coroutines/#0 https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb - Suraj Vaishnav

0

你必须先创建 DownloadXML 类的对象。

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

并返回listOffres。

listOffres = ContainerData.getFeeds();
return listOffers;

0

你可以执行异步任务的方式有:

new DownloadXML().execute();

或者

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

还有一件事,如果您在asynctask中得到了一个数组,那么使用postexecute方法对您的for循环进行迭代是很好的选择

如下所示

  protected void onPostExecute(String s) {

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


    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接