【发布时间】:2017-01-30 16:48:27
【问题描述】:
我正在使用带有 paho 的 MQTT 在 android 上接收和发布消息。
我的 MQTT 初始化有以下代码。
private void initializeMQTT(){
try{
mqttClient = new MqttClient(
"tcp://broker.hivemq.com:1883",
MqttClient.generateClientId(),
new MemoryPersistence()
);
mqttClient.connect();
mqttConnected = mqttClient.isConnected();
mqttClient.subscribe("testtopic/listen",1);
mqttClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) { //Called when the client lost the connection to the broker
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
mqttPayload = topic + ": " + Arrays.toString(message.getPayload());
mqttAnswer.setPayload(mqttPayload.getBytes());
mqttClient.publish("testtopic/publish",mqttAnswer);
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {//Called when a outgoing publish is complete
messageInfoTest = "message was sent";
}
});
}
catch(MqttException e){
}
}
我只是想将收到的消息作为测试发送回其他地方。
现在发生的情况是我第一次在 testtopic/receive 主题上发布。我似乎没有发布任何内容。如果我尝试向 testtopic/receive 发送另一条消息,它永远不会在我的 android 上收到。
有人知道我目前缺少什么吗?
谢谢!
【问题讨论】:
-
这与您上一个问题有何不同? *.com/questions/41882501/…
-
不是。我不知道问题优先级如何在这里起作用。我认为它可能会被埋没,因为我在发布后的第二天对其进行了编辑以添加代码,并认为我会重新发布,以防万一它会引起最初没有代码时可能忽略问题的人的注意.
-
不要多次发布同一个问题,它们只会被重复关闭
-
谢谢,下次我会记住这一点,我删除了其他帖子。
标签: java android mqtt paho hivemq