如何取消当前的ksoap2 HttpTransportSE电话?

时间:2015-12-13 16:42:06

标签: android ksoap2 android-ksoap2

我有以下代码:

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;
    }
}

我想在用户点击某些内容时取消通话。试图通过使用我的活动中调用的cancelTask()方法取消它,但我仍然可以得到catch的错误消息(因为服务器现在已经关闭,所以它无法到达服务器)。

如何取消ksoap来电?

0 个答案:

没有答案