Android 尝试连接到MQTT服务器但服务器未运行时关闭应用程序
下午好,我创建了一个服务来侦听MQTT代理服务器,但是当我启动应用程序而服务器未处于活动状态时,它正在关闭应用程序,当我激活MQTT服务器并打开应用程序时,它工作得非常好。连接到服务器的代码块已经实现了try/catch,但即使如此,它最终还是关闭了应用程序 在服务类中,我执行以下代码:Android 尝试连接到MQTT服务器但服务器未运行时关闭应用程序,android,android-studio,mqtt,paho,Android,Android Studio,Mqtt,Paho,下午好,我创建了一个服务来侦听MQTT代理服务器,但是当我启动应用程序而服务器未处于活动状态时,它正在关闭应用程序,当我激活MQTT服务器并打开应用程序时,它工作得非常好。连接到服务器的代码块已经实现了try/catch,但即使如此,它最终还是关闭了应用程序 在服务类中,我执行以下代码: @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("onStartComma
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("onStartCommand", "onStartCommand");
try {
try {
this.MQTTServer = new MQTTServer(this);
this.MQTTServer.init(new GenericCode.BooleanCode() {
@Override
public void booleanCode(boolean connected) {}
});
} catch (Exception e) {
e.printStackTrace();
}
} catch(Exception e){
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}
服务器未运行时的错误消息:
2020-08-31 11:05:33.172 3099-3099/com.example.ktrine E/ActivityThread: Service com.example.ktrine.service.NotificationService has leaked ServiceConnection org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@8c9622a that was originally bound here
android.app.ServiceConnectionLeaked: Service com.example.ktrine.service.NotificationService has leaked ServiceConnection org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@8c9622a that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1541)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1433)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1620)
at android.app.ContextImpl.bindService(ContextImpl.java:1572)
at android.content.ContextWrapper.bindService(ContextWrapper.java:684)
at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:420)
at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:354)
at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:309)
at com.example.ktrine.server.MQTTServer.init(MQTTServer.java:34)
at com.example.ktrine.service.NotificationService.onStartCommand(NotificationService.java:39)
2020-08-31 11:05:33.172 3099-3099/com.example.ktrine E/ActivityThread:Service com.example.ktrine.Service.NotificationService已泄漏ServiceConnection org.eclipse.paho.android.Service.MqttAndroidClient$MyServiceConnection@8c9622a原来是订在这里的
android.app.ServiceConnectionLeaked:Service com.example.ktrine.Service.NotificationService已泄漏ServiceConnection org.eclipse.paho.android.Service.MqttAndroidClient$MyServiceConnection@8c9622a原来是订在这里的
在android.app.LoadedApk$ServiceDispatcher上
位于android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1433)
位于android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1620)
位于android.app.ContextImpl.bindService(ContextImpl.java:1572)
位于android.content.ContextWrapper.bindService(ContextWrapper.java:684)
位于org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:420)
位于org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:354)
位于org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:309)
位于com.example.ktrine.server.MQTTServer.init(MQTTServer.java:34)
在com.example.ktrine.service.NotificationService.onStartCommand(NotificationService.java:39)上
尽管代码块有一个try/catch,但它正在关闭应用程序。这与在后台运行的服务有关吗?在不知道您的代码的情况下,我的最佳猜测是,
org.eclipse.paho.android.service.MqttAndroidClient
中的某些内容导致了崩溃。很可能是一个丢失的回调。您是否尝试运行附加了调试器的应用程序?可能在mqtserver.init()调用处放置断点。还要检查Logcat.SIdenote的输出:放下double try catch块。