【问题标题】:XML Pull Parser Exception - kSOAP2XML 拉解析器异常 - kSOAP2
【发布时间】:2014-01-07 07:43:01
【问题描述】:

当我尝试使用kSOAP2Android 访问网络服务时,我收到了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 响应,来自 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)

谁能澄清我做错了什么?

【问题讨论】:

  • 如果您可以将完整的堆栈跟踪添加到您的问题中,这将非常有帮助。
  • 我没有任何其他信息。
  • 你能在这里分享你的wsdl吗?
  • @user3090039:分享了我的 WSDL..
  • 你能告诉我你是怎么抓到SOAP response的吗?

标签: java android xml web-services soap


【解决方案1】:

终于解决了。

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

【讨论】:

  • 在不改变命名空间、url 和soapaction 等的情况下可以工作吗?
  • 是的...能够访问服务器...但是,现在我面临无效请求错误。调试中...
  • 即使在 2.3 jar 文件中也有问题吗? sf 服务文件会有所不同吗?
【解决方案2】:

根据您的 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 安卓教程:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 2011-02-23
    相关资源
    最近更新 更多