Android中的MQTT over Websockets

时间:2017-11-29 22:45:30

标签: android websocket mqtt wss

我正在通过网络套接字使用Paho Android Service for MQTT。

https://www.hivemq.com/blog/mqtt-client-library-enyclopedia-paho-android-service

我的问题是如何设置包含查询参数的Broker WSS URI。

例如: wss://test.amazon.com:80 / mqtt?query1 = value& query2 = value

修改

如果我们添加完整的URL,PAHO客户端库将抛出以下异常:

MqttException (0) - java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase()' on a null object reference
W/System.err:     at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
W/System.err:     at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:664)
W/System.err:     at java.lang.Thread.run(Thread.java:761)
W/System.err: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase()' on a null object reference
W/System.err:     at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.receiveHandshakeResponse(WebSocketHandshake.java:133)
W/System.err:     at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.execute(WebSocketHandshake.java:74)
W/System.err:     at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketSecureNetworkModule.start(WebSocketSecureNetworkModule.java:77)
W/System.err:     at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)

修改2

CLIENT = new MqttAndroidClient(this, "wss://test.amazon.com:80/mqtt?query1=value&query2=value", clientId, new MemoryPersistence());
CLIENT.setCallback(this);

MQTT_CONNECTION_OPTIONS = new MqttConnectOptions();
MQTT_CONNECTION_OPTIONS.setCleanSession(true);

try {
    IMqttToken token = CLIENT.connect(MQTT_CONNECTION_OPTIONS);
    token.setActionCallback(new IMqttActionListener() {
        @Override
        public void onSuccess(IMqttToken asyncActionToken) {
            Log.v("tag", "onsuccess");
        }

        @Override
        public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
            exception.printStackTrace();
        }
    });


} catch (MqttException e) {
    e.printStackTrace();
}

1 个答案:

答案 0 :(得分:2)

更改版本

compile'org.eclipse.paho:org.eclipse.paho.client.mqttv3: 1.1.0 '

compile'org.eclipse.paho:org.eclipse.paho.client.mqttv3: 1.2.0 '

解决了这个问题。他们在v1.2.0

中解决了这个问题