我用kotlin链接不上mqtt,我用python可以,说明参数没问题,能不能给个demo

环境

  • EMQX 版本:5.0
  • 操作系统版本:w10

重现此问题的步骤

代码如下package com.example.javamqtt;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private MqttAndroidClient mqttClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EdgeToEdge.enable(this);
    setContentView(R.layout.activity_main);

    // 设置窗口内边距处理逻辑
    ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
        Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
        v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
        return insets;
    });

    // 调用连接MQTT服务器的方法
    connect(this);
}

/**
 * 连接MQTT服务器的方法
 *
 * @param context 上下文对象
 */
public void connect(Context context) {
    // MQTT 服务器的地址
    String broker = "i02f291f.ala.dedicated.tencent.emqxcloud.cn";

// MQTT 服务器的端口号
int port = 1883;
// 发布消息的主题
String topic = “demo/a”;
// 生成带有 ‘pub’ 前缀的随机客户端 ID
String client_id = “mqttx_6bba46b6”;
// MQTT 用户的用户名
String username = “on”;
// MQTT 用户的密码
String password = “123”;

// MQTT 连接选项
MqttConnectOptions options = new MqttConnectOptions();
options.setUserName(username);
options.setPassword(password.toCharArray());

    try {
        // 创建 MQTT 客户端
        MqttAndroidClient client = new MqttAndroidClient(this.getApplicationContext(), "tcp://" + broker + ":" + port, client_id);

        // 设置 MQTT 客户端的回调
        client.setCallback(new MqttCallback() {
            @Override
            public void connectionLost(Throwable cause) {
                // 连接丢失时调用
            }

            @Override
            public void messageArrived(String topic, MqttMessage message) throws Exception {
                // 收到消息时调用
            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken token) {
                // 消息发送完成时调用
            }
        });

        // 连接到 MQTT 服务器
        client.connect(options, null, new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                // 连接成功
                // 可以在这里订阅主题或发布消息
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                // 连接失败
            }
        });
    } catch (MqttException e) {
        e.printStackTrace();
    }

}

}

  1. xxx
  2. xxx
  3. xxx

预期行为

实际行为

不懂这语言:man_facepalming: