我有一个dotNet webservice,每隔几分钟运行一次,返回一些xml数据
我构建了一个ASyncTask类,它将请求数据提交给Web服务,它可以很好地提交数据
然后,Web服务每2分钟提取一次数据以提供响应
我构建了另一个ASyncTask类来访问webservice以获取响应数据,但是当我在计时器上运行ASyncTask时,它会在应用程序首次重试时崩溃,说该任务已在运行
public class GetPaymentsResult extends AsyncTask<Integer, Void, String>{
MainActivity mainActivity = MainActivity.getInstance();
public String SOAP_ACTION = "https://webservice/method";
public String METHOD_NAME = "method";
public String NAMESPACE = "https://webservice/";
public String URL = "https://webservice/payments.asmx";
private SoapObject request;
private HttpTransportSE httpTransport;
private SoapSerializationEnvelope envelope;
public Object response = null;
public static String resultinfo;
@Override
protected String doInBackground(Integer... params){
resultinfo = null;
request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
PropertyInfo pi2 = new PropertyInfo();
pi.setName("RequestID");
pi.setValue(010101);
pi.setType(Integer.class);
request.addProperty(pi);
pi2.setName("APIKey");
pi2.setValue("1234-1234-1234");
pi2.setType(String.class);
request.addProperty(pi2);
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
httpTransport = new HttpTransportSE(URL);
try{
httpTransport.call(SOAP_ACTION, envelope);
response = envelope.getResponse();
}catch(Exception e){
e.printStackTrace();
response = e.getMessage();
}
return response.toString();
}
@Override
protected void onPostExecute(final String result){
super.onPostExecute(result);
mainActivity.getHandler().post(new Runnable() {
@Override
public void run() {
System.out.println(result);
resultinfo = result;
}
});
}
}
我正在使用倒数计时器每十秒左右调用一次这个任务:
public CountDownTimer timer(){
CountDownTimer times = new CountDownTimer(10000, 20) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
checkPaymentreturn();
}
}.start();
return times;
}
我确信我应该采取正确的方式,有人可以帮忙吗?
这是我收到的错误:
**08-09 11:37:21.169 17098-17098/com.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app, PID: 17098
java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:579)
at android.os.AsyncTask.execute(AsyncTask.java:535)
at com.app.ui.CitationFragment$1.onFinish(CitationFragment.java:179)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
答案 0 :(得分:0)
我发现我在类的顶部实例化任务并且每次都从同一个实例调用execute,但是如果我每次想要调用任务时都将任务设置为new,那就像魅力< / p>