2013-04-22 75 views
1

我正在开发android应用程序。我需要关于异步任务doinbackground方法的一些说明。我们可以在android的异步任务的doinbackground方法中一次调用两个方法吗?

Code: 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.main); 

    LongOperation2 op = new LongOperation2(); 
       op.execute(""); 


      } 

    public void test1() { 
      ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
      postParameters.add(new BasicNameValuePair("id", id)); 

      try { 
       res1 = CustomHttpClient.executeHttpPost(
         "http://website.com/folder1/firstpage.php", 
         postParameters); 
       System.out.println("response in test1" + res1.trim()); 

      } 

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

     } 

    public void test2() { 
      ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
      postParameters.add(new BasicNameValuePair("value", value)); 

      try { 
       res2 = CustomHttpClient.executeHttpPost(
         "http://website.com/folder1/secondpage.php", 
         postParameters); 
       System.out.println("response in test2" + res2.trim()); 

      } 

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

     } 

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

      @Override 
      protected String doInBackground(String... params) { 

       test1(); 
             test2(); 
       return "Executed"; 
      } 

      @Override 
      protected void onPostExecute(String result) { 
       dialog1.dismiss(); 
       try { 

               Test.this.startActivity(new Intent(Page1.this, Page2.class)); 
       } 

       catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 

      @Override 
      protected void onPreExecute() { 
       dialog1 = ProgressDialog.show(Test.this, "Please wait...", 
         "Retrieving data ...", true); 
      } 

      @Override 
      protected void onProgressUpdate(Void... values) { 
      } 
     } 

在上面的代码中,我有两个方法test1()和test2()。在这两个方法中,我将参数传递给webservice。现在我的疑问是,我可以在异步任务的doInBackground()中同时调用这两种方法吗?这可以吗?请让我知道或给我这方面的建议。提前致谢。

+2

为什么你在doInBackground中调用两个方法有疑问? – Pragnani 2013-04-22 16:50:58

+0

你想使用asynctask并行执行? – Raghunandan 2013-04-22 16:52:35

+0

您可以始终从doInBackground方法内启动线程并等待它们重新加入... – DigCamara 2013-04-22 16:54:20

回答

3

调用两个或多个方法没有任何错误。但是他们会依次执行。 doBackground方法中没有多处理。

相关问题