gpt4 book ai didi

java - Paho MQTT 抛出异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:35 26 4
gpt4 key购买 nike

为了演示 Paho MQTT,我下载了一个 Java 示例。

 public class Thermometer {

public static final String BROKER_URL = "tcp://test.mosquitto.org:1883";

public static final String TOPIC = "xyz.abc";

private MqttClient client;


public Thermometer() {
try {
MemoryPersistence per = new MemoryPersistence();
String clientId = UUID.randomUUID().toString();
client = new MqttClient(BROKER_URL, clientId, per);
} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}

当我运行它时出现问题,它位于 client = new MqttClient(BROKER_URL, clientId, per);

Exception in thread "main" java.lang.IllegalArgumentException at org.eclipse.paho.client.mqttv3.MqttClient.(MqttClient.java:170) at mqtt_pub.Thermometer.(Thermometer.java:26) at mqtt_pub.Thermometer.main(Thermometer.java:65)

我发现如果 QoS 的值不是 0、1 或 2,但在 MemoryPersistence 类中他们没有提到,@throws IllegalArgumentException。请帮忙,提前谢谢你。

最佳答案

如果您查看 source code MttqClient 你可以看到 uuid 的长度最多只能是 23 个字符。看起来 uuid 更长:

if (clientId == null || clientId.length() == 0 || clientId.length() > 23)  
{
throw new IllegalArgumentException();
}

UUID.randomUUID().toString()返回一个长度为36个字符的字符串;

关于java - Paho MQTT 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28440131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com