猿问

连接到 Mqtt 在线代理时出现 NullPointerException

我正在尝试使用下面的代码和 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);

主题参数是一个包含主题名称的字符串。


弑天下
浏览 206回答 1
1回答

精慕HU

Mosquitto 的 URI 需要协议。看看它的源代码,这是你的异常被抛出的地方,类MqttConnectOpts.java:protected static int validateURI(String srvURI) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; URI vURI = new URI(srvURI);&nbsp; &nbsp; &nbsp; &nbsp; if (!vURI.getPath().equals("")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException(srvURI);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (vURI.getScheme().equals("tcp")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return URI_TYPE_TCP;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (vURI.getScheme().equals("ssl")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return URI_TYPE_SSL;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (vURI.getScheme().equals("local")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return URI_TYPE_LOCAL;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException(srvURI);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (URISyntaxException ex) {&nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException(srvURI);&nbsp; &nbsp; }}因此,它接受 3 种类型的协议前缀:tcp, ssl, local. 关于您的示例,您可以这样尝试:TCP&nbsp;private final String brokerURI = "tcp://test.mosquitto.org:1883";SSLprivate final String brokerURI = "ssl://test.mosquitto.org:8883";
随时随地看视频慕课网APP

相关分类

Java
我要回答