[英]XML Pull Parser Exception - kSOAP2
嘗試使用kSOAP2
從Android
訪問Web服務時,出現XmlPullParserException
。
下面是我的代碼:
NAMESPACE = "http://" + "10.99.60.52" + ":" + "8080" +"/ws/EmployeeServices?wsdl";
URL = "http://" + "10.99.60.52" + ":" + "8080" +"/ws/EmployeeServices";
SOAP_ACTION = "EmployeeServicesPortBinding";
METHOD_NAME = "authorize";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("employeeId", userID);
request.addProperty("localeId", localeID);
request.addProperty("organizationId", organisationID);
request.addProperty("retailLocationId", retailLoationID);
request.addProperty("workstationId", workstationID);
request.addProperty("LoginInput", userID);
request.addProperty("Password", password);
request.addProperty("LoginInputType", loginInputType);
request.addProperty("Privilege", privilege);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
Object obj = envelope.bodyIn;
SoapObject response = null;
if (obj instanceof SoapObject)
response = (SoapObject) (envelope.bodyIn);
responseCode = (response != null) ? response.getProperty("UserAuthorizeResponse").toString() : "Failed";
Toast.makeText(getApplicationContext(), responseCode, Toast.LENGTH_SHORT).show(); //Testing
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_SHORT).show(); //Testing
System.out.println("");
}
WSDL:
<?xml version="1.0" encoding="UTF-8" ?>
- <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.7-b01-.
-->
- <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.7-b01-.
-->
- <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.xx.com/xservices/EmployeeServices" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.xx.com/xservices/EmployeeServices" name="EmployeeServices">
- <types>
- <xsd:schema>
<xsd:import namespace="http://ws.xx.com/xservices/EmployeeServices" schemaLocation="http://10.99.60.52:8080/ws/EmployeeServices?xsd=1" />
</xsd:schema>
</types>
- <message name="changePassword">
<part name="parameters" element="tns:changePassword" />
</message>
- <message name="changePasswordResponse">
<part name="parameters" element="tns:changePasswordResponse" />
</message>
- <message name="getCommissionedAssociates">
<part name="parameters" element="tns:getCommissionedAssociates" />
</message>
- <message name="getCommissionedAssociatesResponse">
<part name="parameters" element="tns:getCommissionedAssociatesResponse" />
</message>
- <message name="authorize">
<part name="parameters" element="tns:authorize" />
</message>
- <message name="authorizeResponse">
<part name="parameters" element="tns:authorizeResponse" />
</message>
- <message name="deleteMessage">
<part name="parameters" element="tns:deleteMessage" />
</message>
- <message name="deleteMessageResponse">
<part name="parameters" element="tns:deleteMessageResponse" />
</message>
- <message name="updateMessage">
<part name="parameters" element="tns:updateMessage" />
</message>
- <message name="updateMessageResponse">
<part name="parameters" element="tns:updateMessageResponse" />
</message>
- <message name="addMessage">
<part name="parameters" element="tns:addMessage" />
</message>
- <message name="addMessageResponse">
<part name="parameters" element="tns:addMessageResponse" />
</message>
- <message name="validateEmp">
<part name="parameters" element="tns:validateEmp" />
</message>
- <message name="validateEmpResponse">
<part name="parameters" element="tns:validateEmpResponse" />
</message>
- <message name="authenticate">
<part name="parameters" element="tns:authenticate" />
</message>
- <message name="authenticateResponse">
<part name="parameters" element="tns:authenticateResponse" />
</message>
- <portType name="EmployeeServices">
- <operation name="changePassword">
<input message="tns:changePassword" />
<output message="tns:changePasswordResponse" />
</operation>
- <operation name="getCommissionedAssociates">
<input message="tns:getCommissionedAssociates" />
<output message="tns:getCommissionedAssociatesResponse" />
</operation>
- <operation name="authorize">
<input message="tns:authorize" />
<output message="tns:authorizeResponse" />
</operation>
- <operation name="deleteMessage">
<input message="tns:deleteMessage" />
<output message="tns:deleteMessageResponse" />
</operation>
- <operation name="updateMessage">
<input message="tns:updateMessage" />
<output message="tns:updateMessageResponse" />
</operation>
- <operation name="addMessage">
<input message="tns:addMessage" />
<output message="tns:addMessageResponse" />
</operation>
- <operation name="validateEmp">
<input message="tns:validateEmp" />
<output message="tns:validateEmpResponse" />
</operation>
- <operation name="authenticate">
<input message="tns:authenticate" />
<output message="tns:authenticateResponse" />
</operation>
</portType>
- <binding name="EmployeeServicesPortBinding" type="tns:EmployeeServices">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <operation name="changePassword">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
- <operation name="getCommissionedAssociates">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
- <operation name="authorize">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
- <operation name="deleteMessage">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
- <operation name="updateMessage">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
- <operation name="addMessage">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
- <operation name="validateEmp">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
- <operation name="authenticate">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
</binding>
- <service name="EmployeeServices">
- <port name="EmployeeServicesPort" binding="tns:EmployeeServicesPortBinding">
<soap:address location="http://10.99.60.52:8080/ws/EmployeeServices" />
</port>
</service>
</definitions>
以下是我從SOAP UI發送的SOAP請求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:emp="http://ws.xx.com/xservices/EmployeeServices">
<soapenv:Header/>
<soapenv:Body>
<emp:authorize>
<!--Optional:-->
<emp:ServiceContext>
<!--Optional:-->
<employeeId>8511</employeeId>
<!--Optional:-->
<localeId>en_US</localeId>
<!--Optional:-->
<organizationId>1</organizationId>
<!--Optional:-->
<retailLocationId>900</retailLocationId>
<!--Optional:-->
<workstationId>1</workstationId>
</emp:ServiceContext>
<!--Zero or more repetitions:-->
<LoginInput>8511</LoginInput>
<!--Optional:-->
<Password>1</Password>
<!--Optional:-->
<LoginInputType>KEYBOARD</LoginInputType>
<!--Optional:-->
<Privilege>SELL_ITEM</Privilege>
</emp:authorize>
</soapenv:Body>
</soapenv:Envelope>
這是我從SOAP UI獲得的SOAP響應:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:authorizeResponse xmlns:ns2="http://ws.xx.com/xservices/EmployeeServices">
<UserAuthorizeResponse>
<userAuthorized>true</userAuthorized>
</UserAuthorizeResponse>
</ns2:authorizeResponse>
</S:Body>
</S:Envelope>
該行拋出以下異常:
androidHttpTransport.call(SOAP_ACTION, envelope);
更新的錯誤:
expected: END_TAG {http://schemas.xmlsoap.org/soap/envelope/}Body (position:END_TAG </{http://schemas.xmlsoap.org/soap/envelope/}S:Fault>@1:314 in java.io.InputStreamReader@40ec86e0)
org.xmlpull.v1.XmlPullParserException: expected: END_TAG {http://schemas.xmlsoap.org/soap/envelope/}Body (position:END_TAG </{http://schemas.xmlsoap.org/soap/envelope/}S:Fault>@1:313 in java.io.InputStreamReader@3e89c3)
at org.kxml2.io.KXmlParser.exception(Unknown Source)
at org.kxml2.io.KXmlParser.require(Unknown Source)
at org.ksoap2.SoapEnvelope.parse(Unknown Source)
at org.ksoap2.transport.Transport.parseResponse(Unknown Source)
at org.ksoap2.transport.HttpTransportSE.call(Unknown Source)
at SOAPClass.main(SOAPClass.java:55)
任何人都可以澄清我在做什么錯嗎?
終於解決了。
由於無效的kSOAP.jar。
我們需要升級到2.5.8。 以前我一直在使用2.5.4。
請參閱: http : //code.google.com/p/ksoap2-android/issues/detail?id=7
JAR鏈接: http : //www.java2s.com/Code/Jar/k/Downloadksoap2androidassembly258jarwithdependenciesjar.htm
根據您的wsdl,您需要更改以下代碼。
NAMESPACE = "http://ws.xx.com/xservices/EmployeeServices/";
URL= "http://10.99.60.52:8080/ws/EmployeeServices";
SOAP_ACTION ="";
讓我知道,您現在要獲得什么,以及是否需要進一步說明。
更新:
NAMESPACE = "http://" + "10.99.60.52" + ":" + "8080" +"/ws/EmployeeServices?wsdl";
上面是您的wsdl url而不是名稱空間,它可以用作URL。
URL = "http://" + "10.99.60.52" + ":" + "8080" +"/ws/EmployeeServices";
這是對的。 這是發布Web服務的端點URL。
SOAP_ACTION = "EmployeeServicesPortBinding";
這是您的綁定名稱,而不是肥皂動作。 你的肥皂動作是空白的。 您可以在綁定下看到。
<soap:operation soapAction="" />
如果空白肥皂操作不起作用,請嘗試將SOAP_ACTION設置為“ namspace / soapAction”。
SOAP_ACTION="http://ws.xx.com/xservices/EmployeeServices/";
如果您有任何困惑,請參考這些在線教程。
kSOAP2 android教程:
http://javatutorialspoint.blogspot.com/2012/02/android-web-service-access-using-ksoap2.html
http://seesharpgears.blogspot.de/2010/11/basic-ksoap-android-tutorial.html
http://stackoverflow.com/questions/11037852/how-to-recieve-a-message-from-a-webservice-with-ksoap2-and-android
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.