我有两种来自SOAP WebService的方法。我将它们称为asyntask,这是info.java页面的超级类别,并以asyntask的Onpost方法获取结果。info.java/oncreate的呼叫代码在下面。
try{
PropertyInfo propertyInfo1 = new PropertyInfo();
properties.clear();
propertyInfo1 = new PropertyInfo();
propertyInfo1.setName("Module_id");
propertyInfo1.setType(String.class);
propertyInfo1.setValue(Utils.selectedModule_id);
properties.add(propertyInfo1);
new Info.AsyncTaskService().execute(new ServiceParams("GetInfo", properties), new ServiceParams("GetInfo_Photo", properties));
} catch (Exception e) {
Toast.makeText(Info.this, "Please check your internet connection.", Toast.LENGTH_LONG);
}
这两种服务方法都采用了相同的属性,这就是为什么我赋予它们相同属性的原因。我的问题是我不能接受结果,因为我知道它需要用订单以不同线程调用这两种方法,但我不知道该怎么做。请问你能帮帮我吗?Asynctask类的代码也在下面,谢谢。
public class AsyncTaskService extends AsyncTask<ServiceParams, Void, Void> {
String resp = "";
String resp2 = "";
ProgressDialog progressDialog;
@Override
protected Void doInBackground(ServiceParams... params) {
resp = WebService.invoke(params[0].properties, params[0].methodName);
resp2 = WebService.invoke(params[1].properties, params[1].methodName);
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.w("WEBSERVICE RESPONSE===", resp);
Log.w("WEBSERVICE RESPONSE===", resp2);
try {
JSONArray ja = new JSONArray(resp);
Utils.subMenuArrayList.clear();
Info_Item info_item=new Info_Item(ja.getJSONObject(0));
((TextView)findViewById(R.id.txtInfo)).setText(info_item.getInfo());
((TextView)findViewById(R.id.txtModule)).setText(Utils.selectedMenuName);
} catch (JSONException e) {
e.printStackTrace();
}
if (progressDialog != null)
progressDialog.dismiss();
}
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(Info.this);
if (progressDialog != null) {
progressDialog.setCancelable(false);
progressDialog.setMessage("İşlem yapılıyor ...");
progressDialog.show();
}
}
protected void onProgressUpdate(Integer... progress) {
if (progressDialog != null)
progressDialog.setProgress(progress[0]);
}
}
我找到了如何做!也想与您分享。
首先描述您的异步任务如下。我有两种方法,我想同时在一个活动中使用这些方法(Paralel(,因此我描述了两个异步任务类。
public class FirstAsyncTask extends AsyncTask<ServiceParams, Void, Void> {
String resp = "";
ProgressDialog progressDialog;
@Override
protected Void doInBackground(ServiceParams... params) {
resp = WebService.invoke(params[0].properties, params[0].methodName);
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.w("WEBSERVICE RESPONSE===", resp);
try {
JSONArray ja = new JSONArray(resp);
Utils.subMenuArrayList.clear();
Info_Item info_item=new Info_Item(ja.getJSONObject(0));
((TextView)findViewById(R.id.txtInfo)).setText(info_item.getInfo());
((TextView)findViewById(R.id.txtModule)).setText(Utils.selectedMenuName);
} catch (JSONException e) {
e.printStackTrace();
}
if (progressDialog != null)
progressDialog.dismiss();
}
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(Info.this);
if (progressDialog != null) {
progressDialog.setCancelable(false);
progressDialog.setMessage("İşlem yapılıyor ...");
progressDialog.show();
}
}
protected void onProgressUpdate(Integer... progress) {
if (progressDialog != null)
progressDialog.setProgress(progress[0]);
}
}
然后,您应该在您的活动的ongreate方法中使用executeOnexecuter调用这样的任务。我在此处使用了一个属性数组来保存要发送到Web服务方法的参数,并描述带有属性和方法名称的ServiceParameter,然后将其发送到ExecuteOneXecuter((方法中。我在两种Web服务方法中都使用了相同的属性,但是您可以描述其他属性数组,例如" private Arraylist属性= new ArrayList&lt;>((;"并添加您需要发送到Web服务方法的参数所需的信息。
try{
PropertyInfo propertyInfo1 = new PropertyInfo();
properties.clear();
propertyInfo1 = new PropertyInfo();
propertyInfo1.setName("Module_id");
propertyInfo1.setType(String.class);
propertyInfo1.setValue(Utils.selectedModule_id);
properties.add(propertyInfo1);
ServiceParams serviceparams=new ServiceParams("GetInfo", properties);
ServiceParams serviceparams2=new ServiceParams("GetInfo_Photo", properties);
FirstAsyncTask asyncTask = new FirstAsyncTask(); // First
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, serviceparams);
SecondAsyncTask asyncTask2 = new SecondAsyncTask(); // Second
asyncTask2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, serviceparams2);
} catch (Exception e) {
Toast.makeText(Info.this, "Please check your internet connection.", Toast.LENGTH_LONG);
}