I have following code:
public class DoTask extends AsyncTask<Object, Object, Object> {
private HttpTransportSE mHttpTransportSE;
public DoTask() {
}
public void cancelTask(){
System.out.println("cancelling task 1");
if(mHttpTransportSE != null){
System.out.println("cancelling task 2");
mHttpTransportSE.reset();
try {
System.out.println("cancelling task 3");
mHttpTransportSE.getServiceConnection().disconnect();
cancel(true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(Object itemImage) {
}
@Override
protected Object doInBackground(Object... arg0) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
mHttpTransportSE = getHttpTransportSE();
mHttpTransportSE.call(SOAP_ACTION, envelope);
mSuccess = true;
}
catch (Exception e){
System.out.println("other exception");
e.printStackTrace();
mSuccess = false;
mErrorMessage = "ERROR!";
}
return null;
}
private final SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setAddAdornments(false);
envelope.setOutputSoapObject(request);
return envelope;
}
private final HttpTransportSE getHttpTransportSE() {
HttpTransportSE ht = new HttpTransportSE(Proxy.NO_PROXY, MAIN_REQUEST_URL, 60000);
ht.debug = true;
ht.setXmlVersionTag("<!--?xml version=\"1.0\" encoding= \"UTF-8\" ?-->");
return ht;
}
private String getValue(String value){
if(value == null || value.equals("anyType{}"))
return "";
return value;
}
}
I want to cancel the call when user clicks something. Tried to cancel it by using the method of cancelTask()
called in my activity but I still can get the error message of catch (because the server is down right now, so it can't reach the server).
How can I cancel the ksoap
call?