How to make the SOAP request

徘徊边缘 提交于 2019-12-05 21:17:59

It looks like KSoap has a hardcoded value for the namespace in 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");
    }

The SoapSerializationEnvelope you're creating here:

envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); 

…is a subclass of SoapEnvelope. So, to change the behaviour, you need to subclass SoapSerializationEnvelope like so:

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

Then, replace the above line with:

envelope = new CustomSoapSerializationEnvelope(SoapEnvelope.VER12); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!