How do I setup a Android MQTT project using ia92 w

2019-08-01 03:04发布

问题:

I have been trying to implement an MQTT service using wmqtt.jar. Like most folks I started with looking at other peoples examples. I found several references to Dale Lane's post http://dalelane.co.uk/blog/?p=1599 and downloaded a version of his service from http://mosquitto.org/2011/11/android-mqtt-example-project/. My problem is that every example I try to run that uses wmqtt.jar in my emulator the code crashes as soon as any class that references MqttSimpleCallback with ajava.lang.ClassNotFoundException. Is there something "special" I have to do to make this run? Normally I would assume the problem is with my code, but since all the examples from other sources I have tried crashes with the same problem, I have to assume I have missed something in my setup?

Here is what my LogCat looks like when the code from mosquitto.org blows up.

04-13 21:48:59.447: D/AndroidRuntime(303): Shutting down VM
04-13 21:48:59.447: W/dalvikvm(303): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-13 21:48:59.548: E/AndroidRuntime(303): FATAL EXCEPTION: main
04-13 21:48:59.548: E/AndroidRuntime(303): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.mosquitto.android.mqttdemo/org.mosquitto.android.mqttdemo.MQTTDemo}: java.lang.ClassNotFoundException: org.mosquitto.android.mqttdemo.MQTTDemo in loader dalvik.system.PathClassLoader[/data/app/org.mosquitto.android.mqttdemo-2.apk]
04-13 21:48:59.548: E/AndroidRuntime(303):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
04-13 21:48:59.548: E/AndroidRuntime(303):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-13 21:48:59.548: E/AndroidRuntime(303):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-13 21:48:59.548: E/AndroidRuntime(303):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-13 21:48:59.548: E/AndroidRuntime(303):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 21:48:59.548: E/AndroidRuntime(303):  at android.os.Looper.loop(Looper.java:123)
04-13 21:48:59.548: E/AndroidRuntime(303):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-13 21:48:59.548: E/AndroidRuntime(303):  at java.lang.reflect.Method.invokeNative(Native Method)
04-13 21:48:59.548: E/AndroidRuntime(303):  at java.lang.reflect.Method.invoke(Method.java:521)
04-13 21:48:59.548: E/AndroidRuntime(303):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-13 21:48:59.548: E/AndroidRuntime(303):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-13 21:48:59.548: E/AndroidRuntime(303):  at dalvik.system.NativeStart.main(Native  Method)
04-13 21:48:59.548: E/AndroidRuntime(303): Caused by: java.lang.ClassNotFoundException: org.mosquitto.android.mqttdemo.MQTTDemo in loader dalvik.system.PathClassLoader[/data/app/org.mosquitto.android.mqttdemo-2.apk]
04-13 21:48:59.548: E/AndroidRuntime(303):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
04-13 21:48:59.548: E/AndroidRuntime(303):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
04-13 21:48:59.548: E/AndroidRuntime(303):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
04-13 21:48:59.548: E/AndroidRuntime(303):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-13 21:48:59.548: E/AndroidRuntime(303):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
04-13 21:48:59.548: E/AndroidRuntime(303):  ... 11 more

回答1:

I'd recommend that you use the Paho Java client library available from http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.java.git/ It's not the same code base as ia92 but the licence is much friendlier.

ia92 is no longer developed or supported, as mentioned on http://mqtt.org/software



回答2:

Make sure you copy the wmqtt.jar and any libraries it might require into your libs directory. Then rebuild it and push it to your emulator.

Missing jar files are a big reason that class def not found exceptions happen.



回答3:

Using eclipse? find the jar in your package explorer, right click it, then choose "Build Path" => "Add To Build path". try again?



标签: android mqtt