嗨,我有一个代码,用于生成对示例 SOAP 服务器的简单请求,我需要在其中构建如下请求:
<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>
i 只需要将“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 无法读取任何名称的前缀,则该服务端的代码很差。 “soapenv”或“v”在您包含的两个 xml 中应被解释为相同。
马辛
关于java - ksoap2 v 任何其他前缀的前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27710169/