Search code examples
androidsoapksoap2ksoap

Android ksoap2 send envelope with empty body?


I want to send an envelope with an empty body like this:

     <v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:d="http://www.w3.org/2001/XMLSchema" 
     xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" 
     xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
     <v:Header />
        <v:Body>
        </v:Body>
     </v:Envelope>

Is it possible and how? I've tried with this:

     HttpsTransportSE transport = new HttpsTransportSE(HOST, PORT, ENDPOINT, TIMEOUT);
     SoapObject request = new SoapObject(null, null);
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);    
     envelope.setOutputSoapObject(request);
     transport.call(SOAP_ACTION, envelope);

I can't seem to figure out how to do it, can anyone please help?


Solution

  • I have fixed the problem by creating my own SoapSerializationEnvelope extending the original one and overridering the writeBody method with this: public class MySoapEnvelope extends SoapSerializationEnvelope

    {   
        public MySoapEnvelope(int version) {
            super(version);
        }
        @Override
        public void writeBody(XmlSerializer writer) throws IOException
        {
            if (bodyOut!=null){
            super.writeBody(writer);
            }   
        }
    }
    

    To use it, you just have to use the MySoapSerializationEnvelope and set envelope.bodyOut=null;

    HttpsTransportSE transport = new HttpsTransportSE(host, port, endpoint, timeout);
    MySoapEnvelope emptySoapEnvelope = new MySoapEnvelope(SoapEnvelope.VER11);
    emptySoapEnvelope.bodyOut = null;
    transport.call(methodname, emptySoapEnvelope);
    

    I dont know if you want to cut a new release with this code. I would possibly make a new method in the original SoapEnvelope called setEmptyEnvelope(). But that's up to you. You could get along with this, but then you should make a respectable documentation so people would know this is how it's done.