我有一个AsyncTask
调用webservice方法并将返回的数据发送回主线程。此任务不会影响任何UI元素,但会在调用CalledFromWrongThreadException
的行上抛出onResultSuccess
。我有许多其他AsyncTask
基本上以完全相同的方式工作,更改被调用的Web服务方法和参数等,但这是唯一失败的。有人发现可能是什么问题吗? AsyncTask
看起来像是:
public static class ClockOff extends AsyncTask<String, Void, Void>
{
public OnAsyncResultClockOff onAsyncResultClockOff;
public void setOnResultListener(OnAsyncResultClockOff onAsyncResultClockOff) {
if(onAsyncResultClockOff != null) {
ResultClockOff = onAsyncResultClockOff;
}
}
private static final String SOAP_ACTION = "http://tempuri.org/ClockOff";
private static final String METHOD_NAME = "ClockOff";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = GlobalFunction.URL;
@Override
protected Void doInBackground(String... strArgs)
{
try
{
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("Items", strArgs[0]);
Request.addProperty("ID", strArgs[1]);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE transport= new HttpTransportSE(URL);
transport.call(SOAP_ACTION, soapEnvelope);
SoapObject result = (SoapObject) soapEnvelope.bodyIn;
String ReturnMessage = ((SoapObject) result.getProperty(0)).getPropertyAsString(0);
String ReturnMessageHeader = ((SoapObject) result.getProperty(0)).getPropertyAsString(1);
String ActionType = ((SoapObject) result.getProperty(0)).getPropertyAsString(2);
String SelectedCompleted = ((SoapObject) result.getProperty(0)).getPropertyAsString(3);
String ClockOffItems = ((SoapObject) result.getProperty(0)).getPropertyAsString(4);
//Exception thrown on the below line
onAsyncResultClockOffItem.onResultSuccess(ReturnMessage, ReturnMessageHeader, ActionType, SelectedCompleted, ClockOffItems);
return null;
}
catch (Exception e)
{
onAsyncResultClockOffItem.onResultFail();
return null;
}
}
}
OnAsyncResultClockOff
只是:
public interface OnAsyncResultClockOff {
public abstract void onResultFail();
public abstract void onResultSuccess(String returnMessage,
String returnMessageHeader, String actionType,
String selectedCompleted, String clockOffItems);
}
通过以下方式调用:
ClockOff co= new ClockOffItem();
co.setOnResultListener(onAsyncResultClockOff);
co.execute(items, ID);
onAsyncResultClockOff
实施OnResultFail()
和OnResultSuccess
方法。