在android中异步任务的doinbackground方法中,我们可以一次调用两个方法吗?

在android中异步任务的doinbackground方法中,我们可以一次调用两个方法吗?,android,Android,我正在开发android应用程序。我需要一些关于异步任务doinbackground方法的澄清 Code: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LongOperation2 op = ne

我正在开发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) {
            }
        }
代码:
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LongOperation2 op=新的LongOperation2();
op.execute(“”);
}
公共void test1(){
ArrayList后参数=新的ArrayList();
添加(新的BasicNameValuePair(“id”,id));
试一试{
res1=CustomHttpClient.executeHttpPost(
"http://website.com/folder1/firstpage.php",
后参数);
System.out.println(“test1中的响应”+res1.trim());
}
捕获(例外e){
e、 printStackTrace();
}
}
公共无效测试2(){
ArrayList后参数=新的ArrayList();
添加(新的BasicNameValuePair(“value”,value));
试一试{
res2=CustomHttpClient.executeHttpPost(
"http://website.com/folder1/secondpage.php",
后参数);
System.out.println(“test2中的响应”+res2.trim());
}
捕获(例外e){
e、 printStackTrace();
}
}
私有类LongOperation2扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
test1();
test2();
返回“已执行”;
}
@凌驾
受保护的void onPostExecute(字符串结果){
对话1.解散();
试一试{
测试。此。开始触觉(新意图(第1页。此,第2页。类));
}
捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
@凌驾
受保护的void onPreExecute(){
dialog1=ProgressDialog.show(Test.this,“请稍候…”,
“正在检索数据…”,true);
}
@凌驾
受保护的void onProgressUpdate(void…值){
}
}

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

调用两个或多个方法没有错。但它们将依次执行。doBackground方法内部没有多处理。

为什么您对在doInBackground中调用两个方法有疑问?您希望使用asynctask并行执行?您可以始终从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) {
            }
        }