我正在开发安卓应用程序。我需要一些关于异步任务 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()。在这两种方法中,我都将参数传递给 web 服务。现在我的疑问是我可以在异步任务的 doInBackground() 中同时调用这两个方法吗?那样可以么?请让我知道或就此提出建议。提前致谢。