【发布时间】:2016-09-08 09:32:09
【问题描述】:
我有两个asynctask,第二个是在第一个asynctask 中调用onPostExecute()。我想显示进度对话框,直到两个 asynctask 都完成。问题是我的progressdialog 只显示几毫秒然后隐藏。我该如何解决这个问题?
//TO call web service save Data on Server
class async_SaveData extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog();
}
@Override
protected String doInBackground(String... params) {
SoapObject request = new SoapObject(namespacegetlistdata, method_name__getlistdata);
cursor = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N'", null);
/*for(int i=0; i<cursor.getCount(); i++)
{*/
int k = 0;
try {
if (cursor.moveToFirst()) {
/*while (cursor.moveToNext())*/
do {
JSONArray arr = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("ConsAcNo", cursor.getString(cursor.getColumnIndex("CONS_ACCOUNT_NO")));
jsonObject.put("MeterSrNo", cursor.getString(cursor.getColumnIndex("MTR_SERIAL_NO")));
//arr.put(jsonObject);
//request.properties.clear();
if (k == 0) {
request.addProperty(parameter_getlistdata, jsonObject.toString());//add the parameters
request.addProperty(parameter_getlistdata1, "XXMFU_APP_INSERTFIELDDATA");//add the parameters
} else {
request.setProperty(0, jsonObject.toString());//add the parameters
request.setProperty(1, "XXMFU_APP_INSERTFIELDDATA");//add the parameters
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//set soap version
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(url_getlistdata);
androidHttpTransport.call(soap_action_getlistdata, envelope); // this is the actual part that will call the webservice
//SoapPrimitive prim = (SoapPrimitive) envelope.getResponse(); // Get the SoapResult from the envelope body.
SoapObject response = (SoapObject) envelope.bodyIn;
} catch (Exception e) {
e.printStackTrace();
Log.d("myError -", String.valueOf(e));
}
k++;
} while (cursor.moveToNext());
}
cursor.close();
} catch (Exception e) {
}
// }
return resultSaveAllData;
}
@Override
protected void onPostExecute(String result1) {
new async_SaveImage().execute();
super.onPostExecute(result1);
}
}
//TO call web service save Image on Server
class async_SaveImage extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
SoapObject request = new SoapObject(namespacegetlistdata, method_name__image);
cursorImage = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N'", null);
/*for(int i=0; i<cursor.getCount(); i++)
{*/
int k = 0;
try {
if (cursorImage.moveToFirst()) {
/*while (cursor.moveToNext())*/
do {
//request.properties.clear();
if (k == 0) {
request.addProperty("fileByte1", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE1")));//add the parameters
request.addProperty("fileName1", cursorImage.getString(cursorImage.getColumnIndex("fileName1")));//add the parameters
} else {
request.setProperty(0, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE1")));//add the parameters
request.setProperty(1, cursorImage.getString(cursorImage.getColumnIndex("fileName1")));//add the parameters
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//set soap version
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(url_getlistdata);
androidHttpTransport.call(soap_action_uploadImage, envelope); // this is the actual part that will call the webservice
//SoapPrimitive prim = (SoapPrimitive) envelope.getResponse(); // Get the SoapResult from the envelope body.
SoapObject response = (SoapObject) envelope.bodyIn;
} catch (Exception e) {
e.printStackTrace();
Log.d("myError -", String.valueOf(e));
}
k++;
} while (cursorImage.moveToNext());
db.execSQL("UPDATE FieldTestDataFinal SET isSend='Y' WHERE isSend='N'");
}
cursorImage.close();
} catch (Exception e) {
}
// }
return resultSaveAllData;
}
@Override
protected void onPostExecute(String result1) {
hideDialog();
super.onPostExecute(result1);
}
}
【问题讨论】:
-
如果两个任务应该同时运行,那你为什么不做一个异步任务呢?
-
在第一个异步任务的预执行时显示进程对话框,并在第二个执行后关闭它
-
我对两个异步任务都有不同的网络服务,我想一秒钟后执行。
-
谢谢希玛尼。我已经和你说的一样了。
标签: android android-asynctask progressdialog