Android uses KSOAP2 to call WebService and correctly import jar packages (GO)
Error message
recently learning Android using KSOAP2 to call the existing WebService method, during the internet to find a lot of code, but every time in the Android emulator run the program will crash, make themselves almost collapsed ....
looked at the error report in Logcat and found that the java.lang.NoClassDefFoundError caused the program to crash:
View Plainprint?
- Error/androidruntime (+): Java.lang.NoClassDefFoundError:org.ksoap2.serialization.SoapObject
- At Webservice.weather.WeatherActivity.getWeather (weatheractivity.java:104)
- At Webservice.weather.WeatherActivity.showWeather (weatheractivity.java:84)
- At Webservice.weather.weatheractivity.access$0 (weatheractivity.java:80)
- At Webservice.weather.weatheractivity$1.onclick (weatheractivity.java:75)
- At Android.view.View.performClick (view.java:2408)
- At Android.view.view$performclick.run (view.java:8816)
- At Android.os.Handler.handleCallback (handler.java:587)
- At Android.os.Handler.dispatchMessage (handler.java:92)
- At Android.os.Looper.loop (looper.java:123)
- At Android.app.ActivityThread.main (activitythread.java:4627)
- At Java.lang.reflect.Method.invokeNative (Native Method)
- At Java.lang.reflect.Method.invoke (method.java:521)
- At Com.android.internal.os.zygoteinit$methodandargscaller.run (zygoteinit.java:868)
- At Com.android.internal.os.ZygoteInit.main (zygoteinit.java:626)
- At Dalvik.system.NativeStart.main (Native Method)
Workaround
Find a lot of information, and finally found that the import of the jar package is a problem .... Importing jar packages In the past when developing Java programs is a way to use:
Project Right click->build path->add libraries->user library-> Select the Library containing the required jar package (click User if not Libraries create a new library and then select it)
However, importing the jar package in Android will cause the program to throw a Java.lang.NoClassDefFoundError exception when it runs in the emulator, and the steps to import the jar package in Android are as follows:
Project Right-click Build path->add External archives-> Select the jar package to import
Another problem is that when the Android emulator accesses the WebService program deployed on Tomcat, it cannot use localhost or the native ip,android default access to the native address is 10.0.2.2.
Reason conjecture
for the reasons for the above noclassdeffounderror, check the Classpath file in the project to find it. If the jar package is imported using the first method, a statement is generated in the Classpath file: <classpathentry kind= "con" path= "org.eclipse.jdt.user_library/ KSOAP2 "/> This method of importing jar packages relies on the development environment eclipse, and if you import the jar package in the second way, the following statement is generated in classpath:
<classpathentry kind= "Lib" path= "D:/javatools/ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar"/> This import is an absolute path that is independent of the Eclipse development environment and, when imported in this way, the jar package information is added to the project Class.dex and then packaged into the APK.
I suspect that the virtual machine is out of the Eclipse development environment when the virtual machine is running, so the first way to rely on an eclipse environment is to import the above error, whereas the second is not dependent on eclipse, Put the jar package information in the Class.dex, so run without error!
The end!!!!
Android uses KSOAP2 to call WebService and correctly import jar packages (GO)