Search code examples
javaandroidksoap2android-ksoap2

How to send a KSOAP2 request using Android?


I've already searched for similar questions but no answer has worked for me

I have this code inside a function that is called from an AsyncTask in android

SoapObject Request=new SoapObject(NAMESPACE, METHOD_NAME3);

Request.addProperty("mac",emac.getText().toString());
Request.addProperty("name",eusu.getText().toString());
Request.addProperty("status",usuData[0]);
Request.addProperty("cEmpSmarpa",usuData[1]);


SoapSerializationEnvelope envelope =  new 
SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;

envelope.headerOut = new Element[1];
envelope.headerOut[0] = buildAuthHeader();

envelope.setOutputSoapObject(Request);


HttpTransportSE transpor = new HttpTransportSE(URL);
transpor.debug = true;
transpor.call(SOAP_ACTION3,envelope);

resultString=(SoapObject) envelope.getResponse();

And with it, I'm sending this XML to my web service

<soapenv:Body>
     <grup:Update>
           <grup:mac>MAC</grup:mac>

           <grup:name>NAME</grup:usuario>

           <grup:status>1</grup:estatus>

           <grup:cEmpSmarpa>0</grup:cEmpresasSmarpa>
     </grup:Update>
</soapenv:Body>

but it is not working since I need to send it like this

<soapenv:Body>
      <grup:Update>    
         <grup:user>
            <grup:mac>MAC</grup:mac>

            <grup:name>NAME</grup:usuario>

            <grup:status>1</grup:estatus>

            <grup:cEmpSmarpa>0</grup:cEmpresasSmarpa>

         </grup:user>
      </grup:Update>
</soapenv:Body>

How can I add that extra group called user that contains my properties?


Solution

  • I did this:

    SoapObject Request=new SoapObject(NAMESPACE, METHOD_NAME3);
    
    SoapObject Soapusuario = new SoapObject(NAMESPACE, "user");
    
                PropertyInfo pi = new PropertyInfo();
                pi.setNamespace(NAMESPACE);
                pi.setType(PropertyInfo.OBJECT_CLASS);
                pi.setName("user");
                pi.setValue("");
                Soapusuario.addProperty(pi);
    
    

    but then I was getting some "i:type=user" field error, so I added

    envelope.implicitTypes = true; 
    

    and that solved it.