-->

How can you remove namespace declarations in ksoap

2019-04-02 20:45发布

问题:

Right off the bat, here is my Soap call implementation, minus the irrelevant bits.

public class MySoapClient implements AbstractSoapClient
{

    private String NAMESPACE = "http://www.examples.com/wsdl/MyService/";
    private String METHOD_NAME = "getPersonDetails";
    private String SOAP_ACTION = "http://www.examples.com/getPersonDetails/";
    String URL = "http://192.168.0.10:8088/mockMyServiceBinding?WSDL";


    public Object process() throws Exception
    {
        SoapSerializationEnvelope envelope = generateEnvelope();
        return responseObject = makeCall(envelope);
    }

    private SoapSerializationEnvelope generateEnvelope()
    {
        // dont set a namespace for the requestobject, otherwise ksoap adds implicit namespaces onto request elements
        SoapObject requestObject = new SoapObject("", METHOD_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.implicitTypes = true;

        requestObject.addProperty("name", "Dave");
        envelope.setOutputSoapObject(requestObject);

        return envelope;
    }

    private Object makeCall(SoapSerializationEnvelope envelope)
    {
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try
        {
            androidHttpTransport.debug = true;
            androidHttpTransport.call(SOAP_ACTION, envelope);
            return envelope.bodyIn;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return e;
        }
    }


}

I think the problem is the SoapObject requestObject = new SoapObject("", METHOD_NAME); part.

If I use SoapObject requestObject = new SoapObject("", METHOD_NAME); Then I get this in the bodyOut :

<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>
        <getPersonDetails xmlns="" id="o0" c:root="1">
            <name>Dave</name>
        </getPersonDetails>
    </v:Body>
</v:Envelope>

If I use SoapObject requestObject = new SoapObject(NAMESPACE, METHOD_NAME); Then I get this in the bodyOut :

<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>
        <n0:getPersonDetails id="o0" c:root="1" xmlns:n0="http://www.examples.com/wsdl/MyService/">
            <name i:type="d:string">Dave</name>
        </n0:getPersonDetails>
    </v:Body>
</v:Envelope>

BUT..SoapUI only accepts the following as a valid XML request :

<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>
        <getPersonDetails>
            <name>Dave</name>
        </getPersonDetails>
    </v:Body>
</v:Envelope>

For some reason, it doesn't like the xmlns="http://www.examples.com/getPersonDetails/" id="o0" c:root="1" part, but I can't find a way to remove it, please help!

So how can I completely remove the xmlns declaration? It feels "dirty" creating a SoapObject and setting namespace to ""

回答1:

Play with

envelope.implicitTypes = true;

as well as

envelope.setAddAdornments(false);

and see if you get it to do what you want. Also keep in mind that in the end the request is totally valid and it will depend on your server if it is okay with it and NOT SoapUI.



回答2:

If still you can't remove or you have to created Nested Request XML element then use this one.

public static SoapSerializationEnvelope getSoapEnvelopeForRegister(String methodName, String nameSpace, Hashtable hashProperty, Hashtable securityQueAns) {
        SoapObject soapObject = new SoapObject(nameSpace, methodName);

        for (Object sKey : hashProperty.keySet()) {
            Object sValue = hashProperty.get(sKey);
            soapObject.addProperty(sKey.toString(), sValue);
        }

        SoapObject item = new SoapObject("", "securityQuestions");
        for (Object sKey : securityQueAns.keySet()) {
            Object sValue = securityQueAns.get(sKey);
            item.addProperty(sKey.toString(), sValue);
        }
        soapObject.addSoapObject(item);

        //Log.d("REQ: Obj: ", soapObject.toString());
        return createSoapEnvelope(soapObject);
    }

Output Req. is like this:

<UNAME>nk</UNAME>
         <MOBILENUMBER>0561063097</MOBILENUMBER>
         <UPWD>p</UPWD>
         <CARDNUMBER>5173950000000105</CARDNUMBER>
         <EMAILID>CARDOPS@MAWARID.AE</EMAILID>
         <CARDHOLDER>AHDAB INTERNATIONAL LUXURY TRANSPORT LLC</CARDHOLDER>
         <DEVICEID>e7fdd833268c96c7</DEVICEID>
         <DOB>1975-01-01</DOB>
         <securityQuestions>
            <SA1>a1</SA1>
            <SA2>a2</SA2>
            <SQ2>What is your first school name?</SQ2>
            <SQ1>Where was your first job?</SQ1>
            <SA3>a3</SA3>
            <SQ3>What is your first school name?</SQ3>
         </securityQuestions>