2

我从 Android 客户端连接到我的网络服务时遇到问题。在我的 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";

主要活动:

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
4

3 回答 3

6

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

So, you can either go to CommandPrompt in your Windows to get the IP address of your system or just use http://10.0.2.2:8080/... instead of using localhost.

private static final String WSDL_URL = "http://10.0.2.2:8080/ReversiGameWS/services/GameWS?wsdl";
于 2013-01-12T17:32:40.310 回答
1

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

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

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

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

于 2013-01-12T14:53:28.283 回答
0

java.lang.VerifyError当您针对与运行时使用的库不同的库进行编译时,可能会出现结果。

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

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

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

于 2013-01-12T15:24:59.857 回答