[英]Exception in thread “main” java.lang.IllegalArgumentException: no NetworkModule installed for scheme “http” of URI “http://xxx.xxx.xxx.xxx:xxxx”
I'm currently using on MQTT eclipse/paho.mqtt.java library for my project.我目前正在为我的项目使用 MQTT eclipse/paho.mqtt.java 库。 this is the link of eclipse/paho.mqtt.java library.这是 eclipse/paho.mqtt.java 库的链接。
https://github.com/eclipse/paho.mqtt.java https://github.com/eclipse/paho.mqtt.java
I wanted to connect with MQTT with http protocol.我想用 http 协议连接 MQTT。 but the original library make for the tcp
protocol.但原始库适用于tcp
协议。 I tried to make a connection with mqtt by making a mqtt client.我试图通过创建一个 mqtt 客户端来与 mqtt 建立连接。 I will show my code in below,我将在下面显示我的代码,
Sorry about using the xxx
symbols in the IP addresses and the URLs.抱歉在 IP 地址和 URL 中使用xxx
符号。
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class MQTT {
public static void main(String[] args) {
String topic = "MQTT Examples";
String content = "Message from MqttPublishSample";
int qos = 2;
//String broker = "tcp://iot.eclipse.org:1883";
String broker = "http://xxx.xxx.xxx.xxx:xxxx";
String clientId = "JavaSample";
MemoryPersistence persistence = new MemoryPersistence();
try {
MqttClient sampleClient = new MqttClient(broker, clientId, null);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
String accessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String pass = "";
char[] charArr = pass.toCharArray();
//I added in here my access token of the server
connOpts.setUserName(accessToken);
connOpts.setPassword(charArr);
System.out.println("Connecting to broker: "+broker);
sampleClient.connect(connOpts);
System.out.println("Connected");
System.out.println("Publishing message: "+content);
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos);
sampleClient.publish(topic, message);
System.out.println("Message published");
sampleClient.disconnect();
System.out.println("Disconnected");
System.exit(0);
} catch(MqttException me) {
System.out.println("reason "+me.getReasonCode());
System.out.println("msg "+me.getMessage());
System.out.println("loc "+me.getLocalizedMessage());
System.out.println("cause "+me.getCause());
System.out.println("excep "+me);
me.printStackTrace();
}
}
}
It shows following error,它显示以下错误,
Exception in thread "main" java.lang.IllegalArgumentException: no NetworkModule installed for scheme "http" of URI "http://xxx.xxx.xxx.xxx:xxxx"
at org.eclipse.paho.client.mqttv3.internal.NetworkModuleService.validateURI(NetworkModuleService.java:70)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:454)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:320)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:315)
at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:227)
at MQTT.main(MQTT.java:17)
So what should I do for this, in example the eclipse had been use tcp
protocol in String broker ="tcp://iot.eclipse.org:1883";
那么我应该怎么做,例如tcp
在String broker ="tcp://iot.eclipse.org:1883";
. . but I want to set the http
for this, I tried to add it as this, String broker = "http://xxx.xxx.xxx.xxx:xxxx";
但我想为此设置http
,我尝试将其添加为, String broker = "http://xxx.xxx.xxx.xxx:xxxx";
then got thte above error, So what should do for this.然后得到了上述错误,那么应该怎么做。
It is not clear from the question what you are trying to do, but this may help clarify a few things for you.从问题中不清楚您要做什么,但这可能有助于为您澄清一些事情。
The in the Paho client broker URL schema (protocols) can be one of the following: Paho 客户端代理中的 URL 架构(协议)可以是以下之一:
tcp://
This is native MQTT over TCP tcp://
这是 TCP 上的本机 MQTTssl://
This is native MQTT over TCP with SSL/TLS ssl://
这是在 TCP 上使用 SSL/TLS 的本机 MQTTws://
This MQTT over Websockets ws://
这个 MQTT over Websocketswss://
This is MQTT over Websockets with SSL/TLS wss://
这是 MQTT over Websockets with SSL/TLSlocal://
special case not useful here. local://
特殊情况在这里没有用。 In order to use anything other than basic MQTT over TCP ( tcp://
) you will need to configure the broker to support this as an extra listener on a different port to the default 1883.为了在 TCP ( tcp://
) 上使用除基本 MQTT 以外的任何东西,您需要将代理配置为在与默认 1883 不同的端口上支持此作为额外的侦听器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.