我有一个蚊子经纪人在我的电脑上运行,mosquitto -v我正在尝试从我的 android 应用程序连接到它。我正在做的是
public void connect() {
mqttAndroidClient = new MqttAndroidClient(context, "mqtt://192.168.1.198:1883", clientId);
mqttAndroidClient.setCallback(callback);
mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setAutomaticReconnect(true);
mqttConnectOptions.setCleanSession(false);
mqttAndroidClient.connect(mqttConnectOptions, context, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
disconnectedBufferOptions.setBufferEnabled(true);
disconnectedBufferOptions.setBufferSize(100);
disconnectedBufferOptions.setPersistBuffer(false);
disconnectedBufferOptions.setDeleteOldestMessages(false);
mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
Log.i(LOG_TAG, "Connected to the broker");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.e(LOG_TAG, "Not able to connect to the broker");
}
});
while(!mqttAndroidClient.isConnected()) {}
try {
mqttAndroidClient.subscribe("notify/new", 0);
} catch (MqttException ex) {
System.err.println("Exception whilst subscribing");
ex.printStackTrace();
}
}
但它永远不会连接,因为我没有看到“已连接到代理”消息并且它卡在了 while 循环中。我究竟做错了什么?