如何在循环内多次调用asyncTask

时间:2015-12-20 12:17:48

标签: android android-asynctask

实际上我想要做的是在循环中多次调用asyncTask。因此,第一次asyncTask将立即开始,从第二次开始,它将检查AsyncTask是否已完成 - 如果已完成,则再次使用不同的值调用它。

以下是我的活动代码:

在onCreate()

btnUpload.setOnClickListener(new View.OnClickListener() 
 {
    @Override
    public void onClick(View v) 
       {
        count_response = 0;
        newUploadWithSeparate();
      }
 });   

newUploadWithSeparate()方法:

private void newUploadWithSeparate()
        {
            responseString_concat = "";

            if(filePath.length > 0)
            {
                for(int i=0;i<filePath.length;i++)
                {
                    count_response = i;

                    if(i == 0)
                    {
                        uploadAsync.execute(filePath[0]);

                        mHandler = new Handler() { 
                            @Override public void handleMessage(Message msg) { 
                                String s=(String)msg.obj;

                                  Log.d("logIMEI","\n Response from Asynctask:  " + s);

                                str_response_fromAsync = s;

                            }
                        };
                    }
                    else
                    {
                        uploadAsync.getStatus();

                        while(uploadAsync.getStatus() == AsyncTask.Status.RUNNING) // this while loop is just to keep the loop  value waitining for finishing the asyncTask
                        {
                            int rx = 0; 
                        }

                        if(uploadAsync.getStatus() != AsyncTask.Status.RUNNING)
                        {
                            if(uploadAsync.getStatus() == AsyncTask.Status.FINISHED)
                                {
                                    if(str_response_fromAsync != "" || !str_response_fromAsync.equals("") || !str_response_fromAsync.isEmpty())
                                       {
                                        uploadAsync.execute(filePath[i]);

                                        x = i;

                                        mHandler = new Handler() { 
                                            @Override public void handleMessage(Message msg) 
                                            { 
                                                String s=(String)msg.obj;

                                                  Log.d("logIMEI","\n Response from Asynctask_" + x + ": "  + s);

                                                str_response_fromAsync = s;

                                            }
                                        };


                                       }
                                }
                        }
                    }

                 }
            }
        }

asyncTask:

private class UploadFileToServer extends AsyncTask<String, Integer, String> 
        {
            @Override
            protected void onPreExecute() 
            {
                super.onPreExecute();
            }


            @Override
            protected String doInBackground(String... params) 
            {
                return uploadFile(params[0]);
            }

            private String uploadFile(String pr)
            {
                //inside here calling webservice and getting a response string as result.        
                    MyWebsrvcClass mycls = new MyWebsrvcClass();
                    return responseString_concat = mycls.Call(xxx,yyy) ;
            } 

            @Override        
            protected void onPostExecute(String result) 
            {
                Log.d("logIMEI" , "\n count_response : "+ count_response + "  fileprath_len : " + filePath.length);

                    Message msg=new Message();
                    msg.obj=result.toString();
                    mHandler.sendMessage(msg);

                super.onPostExecute(result);  
            } 
        }

现在问题是它没有按预期工作。 i的值第一次等于0而不是AsyncTask被调用,之后不再被调用。

另外,当第一次调用AsyncTask时,它仍然没有直接进入onPostExecute()。当循环完全结束并且newUploadWithSeparate()方法结束时,onPostExecute()正在运行。

使用AsyncTask内部循环完成此任务或以其他方式完成此任务的任何解决方案?

0 个答案:

没有答案