我正在尝试使用下面的代码和 Java 中的 Paho 库连接到在线代理https://test.mosquitto.org/ :
private final String brokerURI = "test.mosquitto.org:1883"; //should be changed to 8883 with SSL
try { //tentativo di creazione del client
client = new MqttClient(brokerURI, idClient); <--NullPointerException here
client.setCallback(new ClientCallback(codaTopic, codaMessaggi, finestra)); //set delle callback
setConnectionOptions(); //set delle opzioni connessione
client.connect(opzioni); //connessione al server
} catch (MqttException e) {
System.err.println(e.getMessage());
System.err.println("Connessione fallita Client, riavviare il sistema.");
}
连接选项在这里设置:
private void setConnectionOptions() {
opzioni = new MqttConnectOptions();
opzioni.setAutomaticReconnect(true);
opzioni.setCleanSession(false);
opzioni.setConnectionTimeout(30);
opzioni.setKeepAliveInterval(60);
}
但它NullPointerException在创建MqttClient. 特别是控制台显示:
Exception in thread "Thread-3" java.lang.NullPointerException
at org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateURI(MqttConnectOptions.java:489)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:291)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:185)
at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:226)
at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:138)
at client.Client.run(Client.java:78)
我如何设法连接和使用 SSL?网上冲浪没有任何教程或指南有用,我已经下载了mosquitto.org.crtSSL 连接文件,但我不知道在哪里使用它,也没有找到教程。
编辑
将 BrokerUri 更改为
private final String brokerURI = "tcp://test.mosquitto.org:1883"; //indirizzo broker
在尝试使用指令订阅主题时
client.subscribe(topic, 1);
主题参数是一个包含主题名称的字符串。
精慕HU
相关分类