1

马上,这是我的 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);Then 我在 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);Then 我在 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>

但是..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 并将命名空间设置为“”感觉“脏”

4

2 回答 2

9

envelope.implicitTypes = true;

envelope.setAddAdornments(false);

看看你是否让它做你想做的事。还要记住,最终请求是完全有效的,它取决于你的服务器是否可以接受,而不是 SoapUI。

于 2012-01-20T23:15:07.077 回答
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>
于 2017-01-16T12:35:55.993 回答