嗨,我有一个代码,用于生成一个示例肥皂服务器的简单请求,在该示例中,我需要建立一个请求,例如:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://10.1.5.80:8080/">
<soapenv:Header/>
<soapenv:Body>
<ns:GETSERVERTIME/>
</soapenv:Body>
</soapenv:Envelope>
但是我明白了
<v:Envelope
xmlns:i="http://www.w3.org/1999/XMLSchema-instance"
xmlns:d="http://www.w3.org/1999/XMLSchema"
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body>
<n0:GETSERVERTIME xmlns:n0="http://localhost:8080/" />
</v:Body>
</v:Envelope>
我只需要将“ v:”更改为“ soapenv:”
我的代码:
/**
* Created by Vinicius Gati on 30/12/14.
*
*/
public class ServerSOAP {
private static final String METHOD_NAME = "GETSERVERTIME";
private static final String NAMESPACE = "http://localhost:8080/";
private static final String SOAP_ACTION = "";
private static final String URL = "http://10.1.5.80:8080/ws/SERVERTIME.apw?WSDL";
public static String getServerTime() {
String retorno = "";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.implicitTypes = false;
envelope.setAddAdornments(false);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call( NAMESPACE + METHOD_NAME, envelope );
SoapObject response = (SoapObject) envelope.getResponse();
} catch (Exception e) {
e.printStackTrace();
}
return retorno;
}
}
但是我为之疯狂,却没有成功。
最佳答案
如您所见:
https://github.com/mosabua/ksoap2-android/blob/master/ksoap2-base/src/main/java/org/ksoap2/SoapEnvelope.java
编写方法将前缀定义为字符串常量(来自SoapEnvelope类的代码副本,请参见提供的链接):
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并尝试重新定义此方法以使用“ soapenv”前缀。
顺便说一句:如果WS无法读取任何名称的前缀,则此服务端的代码不正确。在包含的两个xml中,“ soapenv”或“ v”应解释为相同。
马辛