在android Activity中运行多个AsyncTask

时间:2014-10-15 05:44:05

标签: android

我需要从json urls下载一些图片。我有两个url。在这两个url标签是不同的。因此我无法使用相同的方法来处理数据。我可以在同一个类中使用两个AsyncTask操作来下载这个图象?

4 个答案:

答案 0 :(得分:2)

检查一下它会对你有所帮助。

示例活动。

public class AsyncTaskActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String url1= "";
        String url2= "";

        new ExampleAsynTask().execute(url1,url2);  
    }
}

Asyntask:

private class ExampleAsynTask extends AsyncTask<String, Void, String> {


    @Override
    protected void onPreExecute() {
    // Do the Operations like updating the UI in android before the background operation is   finished
    }

    @Override
    protected String doInBackground(String... params) {
        // Perform background operations that take longer time to run
        String url1= params[0]; 

        backgroundTask(params[0])
         backgroundTask(params[1])

        return "Done";
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    // Do the Operations like updating the UI in android during the background operation is running
    }


    @Override
    protected void onPostExecute(String result) {
       // Do the Operations like updating the UI in android after the background operation is finished
    }
    public void backgroundTask(String url){
    //To do coding
    }
}

答案 1 :(得分:0)

使用异步任务,如下所示

class asyncFirst extends AsyncTask<String, Integer, String>{


    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        progressDialog = new ProgressDialog(this);
        progressDialog.show();

    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        try {

            HttpClient client = new DefaultHttpClient();
            URI website = new URI("http:// your first link here ");
            HttpPost request = new HttpPost();

            // Add your code  

            // get your first url response here

            }
            }catch (Exception e) {
                e.printStackTrace();
           }


        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub

        new asyncSecond ().execute();

    }
}


class asyncSecond extends AsyncTask<String, Integer, String>{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        try {

            HttpClient client = new DefaultHttpClient();
            URI website = new URI("http:// your second link here ");
            HttpPost request = new HttpPost();

            // Add your code  

           // get your second url response here 


            }
            }catch (Exception e) {
                e.printStackTrace();
           }


        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub

        // dismiss your progress dialog here

        progressDialog.dismiss();

    }
}

答案 2 :(得分:0)

只需使用一个异步任务,并在其中执行两个服务器请求


示例 ::如果您需要将第一个http-request的结果传递给第二个http-request

,这是最合适的

AsyncTaskActivity.java

public class AsyncTaskActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new LongOperation().execute("");  
    }

    private class LongOperation extends AsyncTask<String, Void, String> {


        @Override
        protected void onPreExecute() {
        // Do the Operations like updating the UI in android before the background operation is   finished
        }

        @Override
        protected String doInBackground(String... params) {
            // Perform background operations that take longer time to run

            backgroundOperNoOne(){
               //Write a Http request for First url contain the image name and the dates
               //Store the value in a local variable if you want to use the data obtained from this network request to next network request
            }

             backgroundOperNoTwo(){
               //Write a Http request for Second url contain the date and the image url
            }

            return "Executed";
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        // Do the Operations like updating the UI in android during the background operation is running
        }


        @Override
        protected void onPostExecute(String result) {
           // Do the Operations like updating the UI in android after the background operation is finished
        }
    }
}

注意 ::

  • 您也可以从second async task拨打first async task 来自onPostExecute
  • first Async Task
  • 如果您想要两个不同的independent async tasks。只需拨打一个 低于另一个,但他们将根据他们的时间独立运行 复杂性
  • 如果您想处理图片,请查看picassoimageLoaderVolly 供您使用

使用单个异步任务总是更好

答案 3 :(得分:0)

是的,你可以,但我建议你使用 ExecutorService 。好像你需要同时下载更多图像,如10或15,然后使用那么多的AsyncTask将是一个问题。

所以

只需制作 Runnable ,例如:

class ImageDownloader implements Runnable{
    public String picID = "";
    public ImageDownloader(String id){
        picID = id;
    }
    @Override
    public void run(){
        // image downloading code
    }
}

并使用 ExecutorService ,如:

ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(new ImageDownloader(somePicID));
相关问题