如何发出SOAP请求

时间:2014-04-19 05:58:59

标签: android soap ksoap2 android-ksoap2

我正在Android中发送SOAP请求。我想要实现的目标是:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">
   <soap:Header/>
   <soap:Body>
      <tem:GeneratePin>

         <tem:sEmail>xyz@abc.com</tem:sEmail>

         <tem:sFlage>true</tem:sFlage>
      </tem:GeneratePin>
   </soap:Body>
</soap:Envelope>

但我得到了:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://www.w3.org/2003/05/soap-encoding" xmlns:v="http://www.w3.org/2003/05/soap-envelope">
<v:Header />
<v:Body>
<tem:GeneratePin xmlns="http://www.tempuri.org/">
<tem:sEmail>abc@xyz.com</tem:sEmail>
<tem:sFlage>true</tem:sFlage>
</tem:GeneratePin>
</v:Body>
</v:Envelope>

我使用以下代码:

static String SOAP_ACTION = "http://tempuri.org/GeneratePin";
    static String namespace = "http://www.tempuri.org/";
    private static String url = "xyz";

    static SoapObject request = null;
    static SoapSerializationEnvelope envelope;
    static HttpTransportSE androidHttpTransport;

    // MethodName variable is define for which webservice function will call
    public static String getPin(String MethodName) {

        try {
            request = new SoapObject(namespace, "tem:GeneratePin");

            /*
             * PropertyInfo pi = new PropertyInfo(); pi.setName("sEmail");
             * pi.setValue("rishabh@vervesys.local"); request.addProperty(pi);
             * 
             * PropertyInfo pi1 = new PropertyInfo(); pi1.setName("sFlage");
             * pi1.setValue("true"); request.addProperty(pi1);
             */

            request.addProperty("tem:sEmail", "abc@xyz.com");
            request.addProperty("tem:sFlage", "true");

            System.out.println("request is : == " + request);
            envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
            envelope.dotNet = true;
            envelope.implicitTypes = true;
            envelope.setAddAdornments(false);
            envelope.encodingStyle = "utf-8";
            envelope.setOutputSoapObject(request);
            /*
             * MarshalDouble marshaldDouble = new MarshalDouble();
             * marshaldDouble.register(envelope);
             */

            androidHttpTransport = new HttpTransportSE(url);
            androidHttpTransport.debug = true;

            androidHttpTransport.call(SOAP_ACTION, envelope);
            System.out
                    .println("ONE!@#$%^& " + androidHttpTransport.requestDump);
            /*
             * androidHttpTransport
             * .setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
             */
            final SoapPrimitive response = (SoapPrimitive) envelope
                    .getResponse();
            Log.i("Webservice Output", response.toString());
            return response.toString();

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

1 个答案:

答案 0 :(得分:3)

看起来KSoap在SoapEnvelope中有一个名称空间的硬编码值:

https://github.com/karlmdavis/ksoap2-android/blob/master/ksoap2-base/src/main/java/org/ksoap2/SoapEnvelope.java#L182

/**
* Writes the complete envelope including header and body elements to the
* given XML writer.
*/
    public void write(XmlSerializer writer) throws IOException {
        writer.setPrefix("i", xsi);
        writer.setPrefix("d", xsd);
        writer.setPrefix("c", enc);
        writer.setPrefix("v", env);
        writer.startTag(env, "Envelope");
        writer.startTag(env, "Header");
        writeHeader(writer);
        writer.endTag(env, "Header");
        writer.startTag(env, "Body");
        writeBody(writer);
        writer.endTag(env, "Body");
        writer.endTag(env, "Envelope");
    }

您在此处创建的SoapSerializationEnvelope

envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); 

...是SoapEnvelope的子类。因此,要更改行为,您需要像这样继承SoapSerializationEnvelope

public class CustomSoapSerializationEnvelope extends SoapSerializationEnvelope {

    @Override
    public void write(XmlSerializer writer) throws IOException {
        writer.setPrefix("i", xsi);
        writer.setPrefix("d", xsd);
        writer.setPrefix("c", enc);
        writer.setPrefix("soap", env); // <-- changed line
        writer.startTag(env, "Envelope");
        writer.startTag(env, "Header");
        writeHeader(writer);
        writer.endTag(env, "Header");
        writer.startTag(env, "Body");
        writeBody(writer);
        writer.endTag(env, "Body");
        writer.endTag(env, "Envelope");
    }
}

然后,将以上行替换为:

envelope = new CustomSoapSerializationEnvelope(SoapEnvelope.VER12);