【问题标题】:Paho MQTT throws ExceptionPaho MQTT 抛出异常
【发布时间】:2015-04-10 23:37:56
【问题描述】:

为了演示 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);

线程“主”java.lang.IllegalArgumentException 中的异常 在 org.eclipse.paho.client.mqttv3.MqttClient.(MqttClient.java:170) 在 mqtt_pub.Thermometer.(Thermometer.java:26) 在 mqtt_pub.Thermometer.main(Thermometer.java:65)

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

【问题讨论】:

标签: java mqtt paho iot


【解决方案1】:

如果您查看MttqClientsource code,您会发现uuid 的长度最多只能为23 个字符。看起来 uuid 更长了:

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

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

【讨论】:

    猜你喜欢
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 2013-05-24
    • 2022-01-03
    • 2016-11-06
    相关资源
    最近更新 更多