Android - 创建SoapObject会抛出InvocationTargetException

时间:2012-06-06 19:57:14

标签: android web-services soap ksoap2 android-ksoap2

我正在学习如何在KSOAP的移动平台(特别是Android)上使用Web服务。但是,在创建SoapObject时,我似乎遇到了问题。

String NAMESPACE = "http://webservicesx.net/";
String METHOD_NAME = "ChangeMetricWeightUnit";
String SOAP_ACTION = NAMESPACE + METHOD_NAME;
String URL = "http://173.201.44.188/convertMetricWeight.asmx";

SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

我的代码在应用程序启动和调试时崩溃,表明SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);会抛出InvocationTargetException

我的LogCat如下:

06-06 19:53:18.900: E/AndroidRuntime(995): FATAL EXCEPTION: main
06-06 19:53:18.900: E/AndroidRuntime(995): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
06-06 19:53:18.900: E/AndroidRuntime(995):  at org.hcfcd.webtest.WebServicesTest.onCreate(WebServicesTest.java:31)
06-06 19:53:18.900: E/AndroidRuntime(995):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-06 19:53:18.900: E/AndroidRuntime(995):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-06 19:53:18.900: E/AndroidRuntime(995):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-06 19:53:18.900: E/AndroidRuntime(995):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-06 19:53:18.900: E/AndroidRuntime(995):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-06 19:53:18.900: E/AndroidRuntime(995):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 19:53:18.900: E/AndroidRuntime(995):  at android.os.Looper.loop(Looper.java:130)
06-06 19:53:18.900: E/AndroidRuntime(995):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-06 19:53:18.900: E/AndroidRuntime(995):  at java.lang.reflect.Method.invokeNative(Native Method)
06-06 19:53:18.900: E/AndroidRuntime(995):  at java.lang.reflect.Method.invoke(Method.java:507)
06-06 19:53:18.900: E/AndroidRuntime(995):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-06 19:53:18.900: E/AndroidRuntime(995):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-06 19:53:18.900: E/AndroidRuntime(995):  at dalvik.system.NativeStart.main(Native Method)

从我从LogCat可以看出,尽管我将KSOAP.jar导入到项目中,编译器仍找不到SoapObject类。

2 个答案:

答案 0 :(得分:0)

问题可能与exception while using ksoap2 library for android问题相同。试试this

答案 1 :(得分:-1)

我不完全确定为什么会这样,但在SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);块中围绕try如此:

try {
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
...
}

导致我的代码正确编译和执行。不太确定为什么简单地将它包围在try块中使它工作,因此对该注释的任何澄清都会很好。