问题
I am making a SOAP request in Android. What exactly I want to achieve is:
<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>
but I am getting:
<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>
I am using the following code:
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:
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);
来源:https://stackoverflow.com/questions/23166499/how-to-make-the-soap-request