如何通过 ksoap2 创建 SOAP 请求
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header/>
<S:Body>
<ns2:FReadStatus xmlns:ns2="http://poweb13/">
<arg0>000D6F0000</arg0>
</ns2:FReadStatus>
</S:Body>
</S:Envelope>
我正在一个android项目中工作,我想使用一些JAX-WS。这些服务是由其他人提供的,所以我无法更改其中的任何内容。我想使用此编写的代码发送上述 SOAP 消息,但在调用它们时我收到的唯一内容是 java.lang.NullPointerException
private static final String NAMESPACE = "http://poweb13/";
private static final String URL = "http://smart.gr:8080/aWESoME/SmartPlugService?wsdl";
private static final String SOAP_ACTION = "SmartPlugService";
private static final String METHOD_NAME = "FReadStatus";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo p1 = new PropertyInfo();
p1.setName("MAC");
p1.setValue("000D6F0000");
p1.setType(myDevice.getmac().toString().getClass());
request.addProperty(p1);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
String result=resultsRequestSOAP.getProperty("return").toString();
Log.i("info","Received :" + result);
} catch (java.lang.ClassCastException e){
SoapFault fault=(SoapFault)envelope.bodyIn;
Log.e("error","Received :" + fault.getMessage().toString());
Log.e("error","Received :" + fault.getLocalizedMessage().toString());
StackTraceElement[] st=fault.getStackTrace();
for(int i=0;i<st.length;i++){
Log.e("error","Received :" +st[i] );
}
} catch (Exception e) {
Log.e("error","smthing went wrong!!");
e.printStackTrace();
}
我认为它没有甚至不创建 xml 文档,但我不知道如何检查它。我尝试创建一个 XmlSerializer 来放置用于创建 xml 的数据,但我也收到 NullPointerException。 那么有人可以帮助我如何编写上述请求吗? 这是 wsdl 文件的某些部分
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://poweb13/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://poweb13/" name="SmartPlugService">
<types>
<xsd:schema>
<xsd:import namespace="http://poweb13/" schemaLocation="http://smart.gr:8080/aWESoME/SmartPlugService?xsd=1"/>
</xsd:schema>
</types>
<message name="FReadStatus">
<part name="parameters" element="tns:FReadStatus"/>
</message>
...
<portType name="SmartPlugService">
<operation name="FReadStatus">
...
<input wsam:Action="http://poweb13/SmartPlugService/FReadStatusRequest" message="tns:FReadStatus"/>
<output wsam:Action="http://poweb13/SmartPlugService/FReadStatusResponse" message="tns:FReadStatusResponse"/>
<fault message="tns:InvalidDeviceAddressException" name="InvalidDeviceAddressException" wsam:Action="http://poweb13/SmartPlugService/FReadStatus/Fault/InvalidDeviceAddressException"/>
<fault message="tns:InternalServiceException" name="InternalServiceException" wsam:Action="http://poweb13/SmartPlugService/FReadStatus/Fault/InternalServiceException"/>
...
</operation>
</portType>
<binding name="SmartPlugServicePortBinding" type="tns:SmartPlugService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
...
<operation name="FReadStatus">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
<fault name="InvalidDeviceAddressException">
<soap:fault name="InvalidDeviceAddressException" use="literal"/>
</fault>
<fault name="InternalServiceException">
<soap:fault name="InternalServiceException" use="literal"/>
</fault>
</operation>
...
</binding>
xsd 文件的某些部分
<xs:schema xmlns:tns="http://poweb13/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://poweb13/">
<xs:element name="FReadStatus" type="tns:FReadStatus"/>
<xs:complexType name="FReadStatus">
<xs:sequence>
<xs:element name="arg0" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
我应该有如下所示的 SOAP 响应:
SOAP Response
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:FReadStatusResponse xmlns:ns2="http://poweb13/">
<return>1</return>
</ns2:FReadStatusResponse>
</S:Body>
</S:Envelope>
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header/>
<S:Body>
<ns2:FReadStatus xmlns:ns2="http://poweb13/">
<arg0>000D6F0000</arg0>
</ns2:FReadStatus>
</S:Body>
</S:Envelope>
i'm working in an android project which i want to consume some JAX-WS. The services are made by someone else so i can't change anything of them.I want to sent the above SOAP message with this written code but the only thing i receive when call them is java.lang.NullPointerException
private static final String NAMESPACE = "http://poweb13/";
private static final String URL = "http://smart.gr:8080/aWESoME/SmartPlugService?wsdl";
private static final String SOAP_ACTION = "SmartPlugService";
private static final String METHOD_NAME = "FReadStatus";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo p1 = new PropertyInfo();
p1.setName("MAC");
p1.setValue("000D6F0000");
p1.setType(myDevice.getmac().toString().getClass());
request.addProperty(p1);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
String result=resultsRequestSOAP.getProperty("return").toString();
Log.i("info","Received :" + result);
} catch (java.lang.ClassCastException e){
SoapFault fault=(SoapFault)envelope.bodyIn;
Log.e("error","Received :" + fault.getMessage().toString());
Log.e("error","Received :" + fault.getLocalizedMessage().toString());
StackTraceElement[] st=fault.getStackTrace();
for(int i=0;i<st.length;i++){
Log.e("error","Received :" +st[i] );
}
} catch (Exception e) {
Log.e("error","smthing went wrong!!");
e.printStackTrace();
}
I think that it doesn't create a xml document even, but i don't know how to check it. I tried to create a XmlSerializer to put data for creating xml but i also receive a NullPointerException.
So can anyone help me about how to code the above request?
here's some parts of wsdl file
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://poweb13/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://poweb13/" name="SmartPlugService">
<types>
<xsd:schema>
<xsd:import namespace="http://poweb13/" schemaLocation="http://smart.gr:8080/aWESoME/SmartPlugService?xsd=1"/>
</xsd:schema>
</types>
<message name="FReadStatus">
<part name="parameters" element="tns:FReadStatus"/>
</message>
...
<portType name="SmartPlugService">
<operation name="FReadStatus">
...
<input wsam:Action="http://poweb13/SmartPlugService/FReadStatusRequest" message="tns:FReadStatus"/>
<output wsam:Action="http://poweb13/SmartPlugService/FReadStatusResponse" message="tns:FReadStatusResponse"/>
<fault message="tns:InvalidDeviceAddressException" name="InvalidDeviceAddressException" wsam:Action="http://poweb13/SmartPlugService/FReadStatus/Fault/InvalidDeviceAddressException"/>
<fault message="tns:InternalServiceException" name="InternalServiceException" wsam:Action="http://poweb13/SmartPlugService/FReadStatus/Fault/InternalServiceException"/>
...
</operation>
</portType>
<binding name="SmartPlugServicePortBinding" type="tns:SmartPlugService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
...
<operation name="FReadStatus">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
<fault name="InvalidDeviceAddressException">
<soap:fault name="InvalidDeviceAddressException" use="literal"/>
</fault>
<fault name="InternalServiceException">
<soap:fault name="InternalServiceException" use="literal"/>
</fault>
</operation>
...
</binding>
Some parts of xsd file
<xs:schema xmlns:tns="http://poweb13/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://poweb13/">
<xs:element name="FReadStatus" type="tns:FReadStatus"/>
<xs:complexType name="FReadStatus">
<xs:sequence>
<xs:element name="arg0" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
i should have a SOAP response like this:
SOAP Response
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:FReadStatusResponse xmlns:ns2="http://poweb13/">
<return>1</return>
</ns2:FReadStatusResponse>
</S:Body>
</S:Envelope>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,你有一个函数 FReadStatus:
它有一个复杂类型(即对象,它是在服务器上找到的类)的元素FReadStatus。
此复杂类型具有 String 类型的属性 arg0。它的定义是:
因此,您需要创建一个实现 kvmSerialized 的本地类,以将此复杂类型映射到服务器上相应的类,因此您将执行以下操作:
现在您已经有了该类,您将在您的代码中执行以下操作:
让我知道发生了什么。您必须使用 Logcat 来检查 requestDump 和 responseDump
< b>更新:回答有关 UnknowHostException 的问题
可能的原因和解决方案
检查您的 AndroidManifest.xml 中是否有:
如果您使用模拟器,请按照此 链接
如果您使用代理,请执行以下操作:
您可能需要使用 dns 预热,请检查此 链接
Ok so you have a function FReadStatus:
which has an element FReadStatus of complex type(ie object, which is a class found on the server).
This complex type has an attribute arg0 of type String. Its definition is:
So you will need to create a local class that implements kvmSerializable to map this complex type to its corresponding class on the server, so you will do :
Now that you have the class , you will do the following in the code you had:
Let me know wt happens. You must use Logcat to check requestDump and responseDump
UPDATE: answering your question about UnknowHostException
possible causes and solutions
Check if in your AndroidManifest.xml you have :
If you are using an emulator do as mentioned in this link
If you are behind a proxy do :
You might need to use warmup the dns , check this link