簡體   English   中英

使用 ActionListener(回調)連接到 MQTT 服務器時出現錯誤異常

[英]Wrong Exception while connecting to MQTT server with an ActionListener (callback)

這是我的代碼:

 public void connect(Context application) throws MqttException {
    Log.d(MainActivity.TAG, "Connecting to MQTT");
    String mqttServerUri = "tcp://18.219.333.193:1883";
    String userName = "xxxxxxx";
    String password = "xxxxxxx";

    MqttAndroidClient client = new MqttAndroidClient(application, mqttServerUri, "mqtt-test");
    MqttConnectOptions options = new MqttConnectOptions();
    options.setCleanSession(false);
    options.setAutomaticReconnect(true);
    options.setKeepAliveInterval(60 * 10);
    options.setMaxInflight(3000);
    options.setUserName(userName);
    options.setPassword(password.toCharArray());

    client.connect(options, iMqttActionListener);

}

這是回調:

    final IMqttActionListener iMqttActionListener = new IMqttActionListener() {
    @Override
    public void onSuccess(IMqttToken asyncActionToken) {
        MqttHandler.isConnected.set(true);
        Log.d(TAG, "[MH] connected to mqtt server");
    }

    @Override
    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
        // Something went wrong e.g. connection timeout or firewall problems
        MqttHandler.isConnected.set(false);
        Log.e(TAG, "[MH] cannot connect to mqtt server", exception);
    }
};

但是在執行 connect 方法時,我得到了這個異常:

[MH] cannot connect to mqtt server
Bad user name or password (4)
    at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:28)
    at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:988)
    at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:145)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:154)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)

這與當用戶名或通行證實際上錯誤時拋出的異常相同。

但是當我替換這一行時:

client.connect(options, iMqttActionListener);

有了這個:

client.connect(options);

它工作正常,但我不知道它何時完成連接,所以它絕對不是用戶/通行證問題。 但我確實需要回調。

我怎樣才能解決這個問題?

PS:我正在使用org.eclipse.paho.client.mqttv3版本: 1.2.0

這不是錯誤,只是 API 設計中的缺陷。

如果你檢查: org.eclipse.paho.android.service.MqttAndroidClient#connect(java.lang.Object, org.eclipse.paho.client.mqttv3.IMqttActionListener)

@Override
public IMqttToken connect(Object userContext, IMqttActionListener callback)
        throws MqttException {
    return connect(new MqttConnectOptions(), userContext, callback);
}

第一個參數不是連接選項,而是用戶上下文。

因此,包括回調,正確的調用將是:

client.connect(options, null, iMqttActionListener);

這很簡單。 Eclipse Paho Client庫有幾個帶有回調的connect()方法。

你可以參考我在MQTT服務類示例中的回答

我希望這可以幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM