简体   繁体   English

org.eclipse.paho.android.service示例代码如何运行?

[英]org.eclipse.paho.android.service how the sample code run?

I am a newbie with android. 我是android的新手。 I want to use org.eclipse.paho.android.service( here ) to connect to the Mqtt Server. 我想使用org.eclipse.paho.android.service( here )连接到Mqtt服务器。 I have read the document and I have implement IMqttActionListener , MqttCallback and MqttTraceHandler . 我已阅读该文档,并已实现IMqttActionListenerMqttCallbackMqttTraceHandler Then I use the MqttAndroidClient to connect to the server, but I can't connect the server. 然后,我使用MqttAndroidClient连接到服务器,但无法连接服务器。 I have debug the android code, but It seems helpless. 我已经调试了android代码,但似乎无奈。

My sample code is below. 我的示例代码如下。

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", "----------------------------------");
}

Always the program will tell me that the client is not connect to the server.I have no idea with that. 程序总是告诉我客户端没有连接到服务器,对此我一无所知。

There is a exception: 有一个例外:

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)?

After debug the app . 调试后的应用程序。 the EXCEPTION is because the mqttService is null.But Now I get another 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)?

My AndroidManifest.xml is 我的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" />

you might want to checkout this example that I've just thrown together. 您可能想查看一下我刚刚一起提出的这个示例。 It's a very simple blank activity but it shows how to create a client, connect to a broker, subscribe & publish to a topic with the basic callbacks. 这是一个非常简单的空白活动,但它显示了如何创建客户端,连接到代理,使用基本回调订阅和发布主题。 https://github.com/jpwsutton/PahoAndroidSample https://github.com/jpwsutton/PahoAndroidSample

The second exception java.lang.SecurityException: ConnectivityService: Neither user 10090 nor current process has android.permission.ACCESS_NETWORK_STATE. 第二个异常java.lang.SecurityException: ConnectivityService: Neither user 10090 nor current process has android.permission.ACCESS_NETWORK_STATE. had been modified.The reason for that maybe because the AndroidManifest.xml is not parse correct.It is weird.And now I can connect to server now.I implements the IMqttActionListener and use the connect.The connect is a asyn function.So When I successfully connect to the server.the app run into IMqttActionListener overwrite function OnSuccess. 已经modified.The原因,也许是因为AndroidManifest.xml中是无法解析correct.It是weird.And现在我可以连接到服务器now.I实现IMqttActionListener和使用connect.The连接是ASYN function.So当我已成功连接到服务器。该应用程序运行到IMqttActionListener覆盖功能OnSuccess中。 My code github 我的代码github

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM