如何在Android中创建kSOAP请求

时间:2015-05-18 14:42:15

标签: android ksoap2

我在下面创建肥皂请求时遇到了麻烦,

<GetCustomerInfoRequest></GetCustomerInfoRequest>

问题是我不知道如何添加

public static Object SOAPCall(String NAMESPACE,String SOAP_ACTION, String METHOD_NAME ,Map<Object,Object> Properties,List<HeaderProperty> Headers) { try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); HttpTransportSE androidHttpTransport = new HttpTransportSE(helper.URL); if(Headers.contains("metoda")) { } Iterator it = Properties.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); request.addProperty(pair.getKey().toString(),pair.getValue().toString()); it.remove(); } envelope.setOutputSoapObject(request); if there is headers if(!Headers.isEmpty()) { Element[] header = new Element[1]; header[0] = new Element().createElement(NAMESPACE1, "auth_info"); Element username = new Element().createElement(null, "session_id"); username.addChild(Node.TEXT, "the session id goes here"); header[0].addChild(Node.ELEMENT, username); envelope.headerOut = header; } // Make the soap call. androidHttpTransport.call(SOAP_ACTION, envelope); // Get the SoapResult from the envelope body. Log.d("REQUESTI",envelope.bodyIn.toString()); return envelope.bodyIn; } catch (Exception aE) { aE.printStackTrace(); return null; } }

作为xml中的父级,然后继续添加比例。 我的ksoap版本是3.3.0

这是我到目前为止编写的代码

{{1}}

1 个答案:

答案 0 :(得分:0)

好的,我想出了答案,我使用嵌套的地图来解决这个问题,这里是java中用于创建上述SOAP请求的代码。

 public static Object NestedSOAPCall(String NAMESPACE,String SOAP_ACTION, String METHOD_NAME ,Map<Object,Map<Object,Object>>
        Properties,List<HeaderProperty> Headers) {

    try
    {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(helper.URL);

        for (Map.Entry<Object, Map<Object, Object>> OuterProperty : Properties.entrySet()) {
            Object OutProperty = OuterProperty.getKey();
            Object OutPropertyValue = OuterProperty.getValue();

            for (Map.Entry<Object, Object> InnerPropery : OuterProperty.getValue().entrySet()) {
                Object InProperty = InnerPropery.getKey();
                Object inPropertyValue = InnerPropery.getValue();
                SoapObject innerRequest = new SoapObject(NAMESPACE,null);
                innerRequest.addProperty(InProperty.toString(),inPropertyValue.toString());


                 request.addProperty(OuterProperty.getKey().toString(),innerRequest);
                //request.addProperty(InProperty.toString(),inPropertyValue.toString());
                Log.d("Request properties", request.toString());
            }
        }



        envelope.setOutputSoapObject(request);

        //if it contains headers
        if(!Headers.isEmpty()) {
            Element[] header = new Element[1];
            header[0] = new Element().createElement(NAMESPACE1, "auth_info"); //AuthHeader is the class name of my authentication
            Element username = new Element().createElement(null, "session_id");
            username.addChild(Node.TEXT, "8496236bf17632fc2d1fb260478b09f2");
            header[0].addChild(Node.ELEMENT, username);
            envelope.headerOut = header;
        }



        // Make the soap call.
        androidHttpTransport.call(SOAP_ACTION, envelope);
        // Get the SoapResult from the envelope body.

        return  envelope.bodyIn;

    }
    catch (Exception aE)
    {
        aE.printStackTrace();
        return null;
    }



}