连接到webservice时,Android应用程序崩溃

时间:2013-01-12 14:50:24

标签: android jax-ws

从Android客户端连接到我的web服务时遇到问题。在我的MainActivity类中,程序在以下行崩溃:

 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

问题是定义上不是方法名称,而是关于命名空间的东西,但我不知道出了什么问题。

GameAndroidUtil:

private static final String NAMESPACE = "http://game.webcentral.pl/";
private static final String SOAP_ACTION = "";
private static final String WSDL_URL = "http://localhost:8080/ReversiGameWS/services/GameWS?wsdl";

MainActivity:

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            GameAndroidUtil.testGameWS();
        } catch (SoapFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

我的GameWS.wsdl:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://game.webcentral.pl" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://game.webcentral.pl" xmlns:intf="http://game.webcentral.pl" xmlns:tns1="http://data.game.webcentral.pl" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
 <wsdl:types>
  <schema elementFormDefault="qualified" targetNamespace="http://game.webcentral.pl" xmlns="http://www.w3.org/2001/XMLSchema">
   <import namespace="http://data.game.webcentral.pl"/>
   <element name="addMove">
    <complexType>
     <sequence>
      <element name="sessionId" type="xsd:string"/>
      <element name="move" type="xsd:int"/>
     </sequence>
    </complexType>
   </element>
   <element name="addMoveResponse">
    <complexType/>
   </element>
   <element name="fault" type="tns1:GameException"/>
   <element name="login">
    <complexType>
     <sequence>
      <element name="userName" type="xsd:string"/>
     </sequence>
    </complexType>
   </element>
   <element name="loginResponse">
    <complexType>
     <sequence>
      <element name="loginReturn" type="xsd:string"/>
     </sequence>
    </complexType>
   </element>
   <element name="getGameStatus">
    <complexType>
     <sequence>
      <element name="sessionId" type="xsd:string"/>
     </sequence>
    </complexType>
   </element>
   <element name="getGameStatusResponse">
    <complexType>
     <sequence>
      <element name="getGameStatusReturn" type="tns1:GameStatus"/>
     </sequence>
    </complexType>
   </element>
  </schema>
  <schema elementFormDefault="qualified" targetNamespace="http://data.game.webcentral.pl" xmlns="http://www.w3.org/2001/XMLSchema">
   <complexType name="GameException">
    <sequence/>
   </complexType>
   <complexType name="GameStatus">
    <sequence>
     <element name="lastMove" nillable="true" type="xsd:int"/>
     <element name="yourMove" type="xsd:boolean"/>
    </sequence>
   </complexType>
  </schema>
 </wsdl:types>

   <wsdl:message name="loginResponse">

      <wsdl:part element="impl:loginResponse" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="loginRequest">

      <wsdl:part element="impl:login" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="GameException">

      <wsdl:part element="impl:fault" name="fault">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="addMoveRequest">

      <wsdl:part element="impl:addMove" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="addMoveResponse">

      <wsdl:part element="impl:addMoveResponse" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="getGameStatusRequest">

      <wsdl:part element="impl:getGameStatus" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="getGameStatusResponse">

      <wsdl:part element="impl:getGameStatusResponse" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:portType name="GameWS">

      <wsdl:operation name="addMove">

         <wsdl:input message="impl:addMoveRequest" name="addMoveRequest">

       </wsdl:input>

         <wsdl:output message="impl:addMoveResponse" name="addMoveResponse">

       </wsdl:output>

         <wsdl:fault message="impl:GameException" name="GameException">

       </wsdl:fault>

      </wsdl:operation>

      <wsdl:operation name="login">

         <wsdl:input message="impl:loginRequest" name="loginRequest">

       </wsdl:input>

         <wsdl:output message="impl:loginResponse" name="loginResponse">

       </wsdl:output>

      </wsdl:operation>

      <wsdl:operation name="getGameStatus">

         <wsdl:input message="impl:getGameStatusRequest" name="getGameStatusRequest">

       </wsdl:input>

         <wsdl:output message="impl:getGameStatusResponse" name="getGameStatusResponse">

       </wsdl:output>

         <wsdl:fault message="impl:GameException" name="GameException">

       </wsdl:fault>

      </wsdl:operation>

   </wsdl:portType>

   <wsdl:binding name="GameWSSoapBinding" type="impl:GameWS">

      <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

      <wsdl:operation name="addMove">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="addMoveRequest">

            <wsdlsoap:body use="literal"/>

         </wsdl:input>

         <wsdl:output name="addMoveResponse">

            <wsdlsoap:body use="literal"/>

         </wsdl:output>

         <wsdl:fault name="GameException">

            <wsdlsoap:fault name="GameException" use="literal"/>

         </wsdl:fault>

      </wsdl:operation>

      <wsdl:operation name="login">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="loginRequest">

            <wsdlsoap:body use="literal"/>

         </wsdl:input>

         <wsdl:output name="loginResponse">

            <wsdlsoap:body use="literal"/>

         </wsdl:output>

      </wsdl:operation>

      <wsdl:operation name="getGameStatus">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="getGameStatusRequest">

            <wsdlsoap:body use="literal"/>

         </wsdl:input>

         <wsdl:output name="getGameStatusResponse">

            <wsdlsoap:body use="literal"/>

         </wsdl:output>

         <wsdl:fault name="GameException">

            <wsdlsoap:fault name="GameException" use="literal"/>

         </wsdl:fault>

      </wsdl:operation>

   </wsdl:binding>

   <wsdl:service name="GameWSService">

      <wsdl:port binding="impl:GameWSSoapBinding" name="GameWS">

         <wsdlsoap:address location="http://localhost:8080/ReversiGameWS/services/GameWS"/>

      </wsdl:port>

   </wsdl:service>

</wsdl:definitions>

来自Web服务的我的GameWS.java:

package pl.webcentral.game;

import java.util.HashMap;

@WebService(targetNamespace = "http://game.webcentral.pl/", portName = "GameWS",        serviceName = "GameWSService")
public class GameWS {

// Further code

}

我做错了什么?我真的尝试了一切。 Web服务本身工作正常。

更新了LogCat:

    01-12 16:54:27.930: E/AndroidRuntime(1070): FATAL EXCEPTION: main
01-12 16:54:27.930: E/AndroidRuntime(1070): java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.webcentral.androidclient1/pl.webcentral.androidclient1.MainActivity}: java.lang.RuntimeException: Unexpected exception
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.os.Looper.loop(Looper.java:137)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at java.lang.reflect.Method.invokeNative(Native Method)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at java.lang.reflect.Method.invoke(Method.java:511)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at dalvik.system.NativeStart.main(Native Method)
01-12 16:54:27.930: E/AndroidRuntime(1070): Caused by: java.lang.RuntimeException: Unexpected exception
01-12 16:54:27.930: E/AndroidRuntime(1070):     at pl.webcentral.androidclient1.GameAndroidUtil.callGameLogin(GameAndroidUtil.java:76)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at pl.webcentral.androidclient1.GameAndroidUtil.testGameWS(GameAndroidUtil.java:18)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at pl.webcentral.androidclient1.MainActivity.onCreate(MainActivity.java:24)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.Activity.performCreate(Activity.java:5008)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-12 16:54:27.930: E/AndroidRuntime(1070):     ... 11 more
01-12 16:54:27.930: E/AndroidRuntime(1070): Caused by: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8080) after 20000ms: isConnected failed: ECONNREFUSED (Connection refused)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.io.IoBridge.isConnected(IoBridge.java:224)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.io.IoBridge.connect(IoBridge.java:112)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at java.net.Socket.connect(Socket.java:842)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:120)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:176)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at pl.webcentral.androidclient1.GameAndroidUtil.callGameLogin(GameAndroidUtil.java:69)
01-12 16:54:27.930: E/AndroidRuntime(1070):     ... 16 more
01-12 16:54:27.930: E/AndroidRuntime(1070): Caused by: libcore.io.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.io.IoBridge.isConnected(IoBridge.java:208)
01-12 16:54:27.930: E/AndroidRuntime(1070):     ... 36 more
01-12 16:54:32.910: I/Process(1070): Sending signal. PID: 1070 SIG: 9

3 个答案:

答案 0 :(得分:6)

您正在尝试通过将localhost:8080指定为托管服务的系统的地址来连接到Web服务。 Android模拟器在虚拟机(QEMU)内运行。因此,localhost:8080将是模拟器自己的环回地址,而不是系统的地址。

因此,您可以转到Windows中的CommandPrompt获取系统的IP地址,或者只使用http://10.0.2.2:8080/...而不是localhost

private static final String WSDL_URL = "http://10.0.2.2:8080/ReversiGameWS/services/GameWS?wsdl";

答案 1 :(得分:1)

问题很可能是您尝试在UI线程上执行Web调用。

检查你的logcat,你可能会看到这个错误。

您不需要长时间运行调用(如Web服务调用)而不需要UI线程,并且需要使用AsyncTasks将它们移出线程。

如果不是这种情况,则需要包含logcat输出,以便我们了解更多信息。

答案 2 :(得分:0)

当您针对不同的库编译时,

java.lang.VerifyError可能是您在运行时使用的结果。

例如,当我尝试运行针对Xerces 1编译的程序时发生了这种情况,但在类路径中找到了Xerces 2。在运行时找到了所需的类(在org.apache.*名称空间中),因此ClassNotFoundException 结果。对类和方法进行了更改,因此在运行时找到的方法签名与编译时的方法签名不匹配。

通常,编译器会标记方法签名不匹配的问题。当加载类时,DVM将再次验证字节码,并在字节码尝试执行不应允许的操作时抛出VerifyError - 比如调用返回String的方法然后存储该返回包含List的字段中的值。

请确保您在编译时使用任何jar或库的正确版本。