Android uses KSOAP2 to invoke WebService and correctly import jar package issues

Source: Internet
Author: User

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?
  1. Error/androidruntime (+): Java.lang.NoClassDefFoundError:org.ksoap2.serialization.SoapObject
  2. At Webservice.weather.WeatherActivity.getWeather (weatheractivity.java:104)
  3. At Webservice.weather.WeatherActivity.showWeather (weatheractivity.java:84)
  4. At Webservice.weather.weatheractivity.access$0 (weatheractivity.java:80)
  5. At Webservice.weather.weatheractivity$1.onclick (weatheractivity.java:75)
  6. At Android.view.View.performClick (view.java:2408)
  7. At Android.view.view$performclick.run (view.java:8816)
  8. At Android.os.Handler.handleCallback (handler.java:587)
  9. At Android.os.Handler.dispatchMessage (handler.java:92)
  10. At Android.os.Looper.loop (looper.java:123)
  11. At Android.app.ActivityThread.main (activitythread.java:4627)
  12. At Java.lang.reflect.Method.invokeNative (Native Method)
  13. At Java.lang.reflect.Method.invoke (method.java:521)
  14. At Com.android.internal.os.zygoteinit$methodandargscaller.run (zygoteinit.java:868)
  15. At Com.android.internal.os.ZygoteInit.main (zygoteinit.java:626)
  16. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.