从处理程序android调用asynctask

时间:2014-02-19 09:22:45

标签: java android android-asynctask

我在为新应用做一些测试时遇到了问题。 我有一个活动,我执行asynctask与我的服务器通信,并要求一个json文件。

我希望定期下载此文件(不知道,3-4秒),所以我在活动中创建了一个处理程序,每次都执行它。

它可以正常使用Logs,但每当我尝试执行asynctask时,都会出现例外情况:

02-19 10:16:41.510: WARN/System.err(9051): java.lang.IllegalStateException: Cannot execute task: the task is already running.
02-19 10:16:41.510: WARN/System.err(9051): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:576)
02-19 10:16:41.510: WARN/System.err(9051): at android.os.AsyncTask.execute(AsyncTask.java:535)
02-19 10:16:41.510: WARN/System.err(9051): at com.mobile.myPacket.mySecondActivity.askForData(mySecondActivity.java:68)
02-19 10:16:41.510: WARN/System.err(9051): at com.mobile.myPacket.mySecondActivity.access$000(mySecondActivity.java:22)
02-19 10:16:41.510: WARN/System.err(9051): at com.mobile.myPacket.mySecondActivity$1.run(mySecondActivity.java:56)
02-19 10:16:41.510: WARN/System.err(9051): at android.os.Handler.handleCallback(Handler.java:733)
02-19 10:16:41.510: WARN/System.err(9051): at android.os.Handler.dispatchMessage(Handler.java:95)
02-19 10:16:41.510: WARN/System.err(9051): at android.os.Looper.loop(Looper.java:136)
02-19 10:16:41.510: WARN/System.err(9051): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-19 10:16:41.510: WARN/System.err(9051): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 10:16:41.510: WARN/System.err(9051): at java.lang.reflect.Method.invoke(Method.java:515)
02-19 10:16:41.510: WARN/System.err(9051): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-19 10:16:41.510: WARN/System.err(9051): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-19 10:16:41.510: WARN/System.err(9051): at dalvik.system.NativeStart.main(Native Method)    

我认为这是因为它是另一个线程,第二个调用正在解决第一个问题。但我不知道如何解决它。

这是我的活动:

            package com.mobile.myPacket;

            import android.app.Activity;
            import android.os.Bundle;
            import android.os.Handler;
            import android.util.Log;
            import android.view.LayoutInflater;
            import android.view.View;
            import android.view.ViewGroup;
            import com.mobile.clases.AsyncResponse;
            import com.mobile.clases.MyTask;


            import android.os.Bundle;
            import android.view.LayoutInflater;
            import android.view.View;
            import android.view.ViewGroup;
            import org.json.JSONArray;
            import org.json.JSONException;
            import org.json.JSONObject;

            public class SecondActivity extends Activity implements AsyncResponse {

                private String urlServer = "url";
                MyTask asyncTask =new MyTask();
                Handler handler = new Handler();


                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    handler.post(sendData);
                    setContentView(R.layout.secondActivity);
                    asyncTask.delegate = this;
                    //We ask for data the first time
                    askForData();
                }


                @Override
                public void processFinish(JSONArray output) {    //Executed from asyncTask
                    //Data received
                    Log.i("TEST", output.toString());
                }

                @Override
                protected void onDestroy() {
                    super.onDestroy();
                    handler.removeCallbacks(sendData);
                }

                private final Runnable sendData = new Runnable(){
                    public void run(){
                        try {
                            //prepare and send the data here..

                            askForData();
                           //Log.i("TEST","aa");
                                    handler.postDelayed(this, 3000);
                        }
                        catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };

                private void askForData(){
                    Log.i("TEST","askForData");
                    asyncTask.execute(urlServer+"test.php");
                }


            }

没有处理程序,asyncTask只运行一次。所以我猜这个课程还可以。

2 个答案:

答案 0 :(得分:2)

您不能多次执行相同的AsyncTask

在执行AsyncTask之前,首先实例化一个新实例:

private void askForData(){
    asyncTask = new MyTask();
    asyncTask.delegate = this;
    asyncTask.execute(urlServer+"test.php");
}

同时删除课程开头AsyncTask的实例化。

答案 1 :(得分:0)

试试这个,

private void askForData(){

if (task.getStatus() == AsyncTask.Status.FINISHED) {
    task = new AsyncTask();
}

if (task.getStatus() != AsyncTask.Status.RUNNING) {
    task.execute();
}
}