如何在ksoap中删除命名空间声明?

时间:2012-01-20 23:08:49

标签: android xml ksoap2 soapui android-ksoap2

马上,这是我的Soap调用实现,减去不相关的位。

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


}

我认为问题是SoapObject requestObject = new SoapObject("", METHOD_NAME);部分。

如果我使用SoapObject requestObject = new SoapObject("", METHOD_NAME);那么我会在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>

如果我使用SoapObject requestObject = new SoapObject(NAMESPACE, METHOD_NAME);那么我会在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仅接受以下内容作为有效的XML请求:

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

出于某种原因,它不喜欢xmlns="http://www.examples.com/getPersonDetails/" id="o0" c:root="1"部分,但我找不到删除它的方法,请帮忙!

那我怎样才能完全删除xmlns声明?感觉“脏”创建一个SoapObject并将命名空间设置为“”

2 个答案:

答案 0 :(得分:9)

envelope.implicitTypes = true;

以及

envelope.setAddAdornments(false);

看看你能不能做你想做的事。还要记住,最终请求是完全有效的,如果它可以使用它而不是SoapUI,它将取决于你的服务器。

答案 1 :(得分:0)

如果仍然无法删除,或者您必须创建嵌套请求XML元素,请使用此元素。

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

输出要求是这样的:

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