XML Pull Parser异常 - kSOAP2

时间:2014-01-07 07:43:01

标签: java android xml web-services soap

我尝试使用XmlPullParserExceptionAndroid点击网络服务时收到kSOAP2

以下是我的代码:

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响应,来自SOAP UI:

<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)

任何人都可以澄清我做错了吗?

2 个答案:

答案 0 :(得分:4)

终于解决了。

由于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

答案 1 :(得分:0)

根据你的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 not namespace,它可以用作URL。

URL = "http://" + "10.99.60.52" + ":" + "8080" +"/ws/EmployeeServices"; 

这是对的。它是您的web服务发布的端点URL。

SOAP_ACTION = "EmployeeServicesPortBinding";

这是您的绑定名称,而不是soap操作。你的肥皂行动是空白的。你可以在绑定下看到。

<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