我是android的新手。我想使用org.eclipse.paho.android.service(here)连接到Mqtt服务器。我已阅读该文档,并实施了IMqttActionListener
,MqttCallback
和MqttTraceHandler
。然后我使用MqttAndroidClient连接到服务器,但我无法连接服务器。我已经调试了android代码,但似乎无能为力。
我的示例代码如下。
ActionListener acListener = new ActionListener(this.context,ActionListener.Action.CONNECT,this.clientHandle,null);
MqttConnectOptions connOpt = new MqttConnectOptions();
//connOpt.setConnectionTimeout(10);
connOpt.setKeepAliveInterval(1000);
this.client.setCallback(new MqttCallbackHandler(this.context,this.clientHandle));
this.client.setTraceCallback(new MqttTraceCallback());
try{
this.client.connect(connOpt,null, acListener);
} catch (MqttException e){
Log.e(this.getClass().getCanonicalName(),"----------------------------------------",e);
}
if(this.client.isConnected()) {
Log.i("connect", "------------------------------------");
} else {
*Log.i("is not connect", "----------------------------------");
}
程序总是会告诉我客户端没有连接到服务器。我根本不知道。
有一个例外:
com.example.zhangkai.gpstraker E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zhangkai.gpstraker/com.example.zhangkai.gpstraker.GPSActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2312)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1346)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5320)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:851)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at org.eclipse.paho.android.service.MqttAndroidClient.publish(MqttAndroidClient.java:812)
at org.eclipse.paho.android.service.MqttAndroidClient.publish(MqttAndroidClient.java:668)
at com.example.zhangkai.gpstraker.MqttConnection.connect(MqttConnection.java:128)
at com.example.zhangkai.gpstraker.GPSActivity.onCreate(GPSActivity.java:27)
at android.app.Activity.performCreate(Activity.java:5265)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2276)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2364)?
at android.app.ActivityThread.access$600(ActivityThread.java:162)?
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1346)?
at android.os.Handler.dispatchMessage(Handler.java:99)?
at android.os.Looper.loop(Looper.java:153)?
at android.app.ActivityThread.main(ActivityThread.java:5320)?
at java.lang.reflect.Method.invokeNative(Native Method)?
at java.lang.reflect.Method.invoke(Method.java:511)?
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:851)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)?
at dalvik.system.NativeStart.main(Native Method)?
调试应用程序后。 EXCEPTION是因为mqttService为null。但是现在我得到另一个异常
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x8000010 (has extras) } in org.eclipse.paho.android.service.MqttService$NetworkConnectionIntentReceiver@415460e8
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:820)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5320)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:851)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
sed by: java.lang.SecurityException: ConnectivityService: Neither user 10090 nor current process has android.permission.ACCESS_NETWORK_STATE.
at android.os.Parcel.readException(Parcel.java:1425)
at android.os.Parcel.readException(Parcel.java:1379)
at android.net.IConnectivityManager$Stub$Proxy.getActiveNetworkInfo(IConnectivityManager.java:720)
at android.net.ConnectivityManager.getActiveNetworkInfo(ConnectivityManager.java:570)
at org.eclipse.paho.android.service.MqttService.isOnline(MqttService.java:814)
at org.eclipse.paho.android.service.MqttService$NetworkConnectionIntentReceiver.onReceive(MqttService.java:796)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:808)
at android.os.Handler.handleCallback(Handler.java:725)?
at android.os.Handler.dispatchMessage(Handler.java:92)?
at android.os.Looper.loop(Looper.java:153)?
at android.app.ActivityThread.main(ActivityThread.java:5320)?
at java.lang.reflect.Method.invokeNative(Native Method)?
at java.lang.reflect.Method.invoke(Method.java:511)?
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:851)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)?
at dalvik.system.NativeStart.main(Native Method)?
我的AndroidManifest.xml是
<uses-permission android:name="android.permission.INTERNET" />
<uses_permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses_permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
答案 0 :(得分:1)
你可能想要查看我刚刚抛出的这个例子。这是一个非常简单的空白活动,但它显示了如何创建客户端,连接到经纪人,订阅&amp;发布到具有基本回调的主题。 https://github.com/jpwsutton/PahoAndroidSample
答案 1 :(得分:0)
第二个异常java.lang.SecurityException: ConnectivityService: Neither user 10090 nor current process has android.permission.ACCESS_NETWORK_STATE.
已经被修改。原因可能是因为AndroidManifest.xml没有解析正确。这很奇怪。现在我可以连接到服务器了。我实现了{{1}并使用connect。连接是一个asyn函数。当我成功连接到服务器时,应用程序运行IMqttActionListener
覆盖函数OnSuccess。
我的代码github