Paho MQTT not connecting Android studio to the Mosquitto broker

99封情书 提交于 2020-06-23 13:20:11

问题


I hosted Mosquitto MQTT broker on Digital Ocean Cloud. Now, I am trying to connect it to the android studio. But it is not connecting to the MQTT broker. I have printed the throwable exception details so the error could be debugged.Please guide me on how this error could be solved. I tried with the following code.

public class MainActivity extends AppCompatActivity {

public MqttAndroidClient CLIENT;
public MqttConnectOptions MQTT_CONNECTION_OPTIONS;


@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String clientId = MqttClient.generateClientId();
    MqttAndroidClient client =
            new MqttAndroidClient(this.getApplicationContext(), "tcp://instrux.live:1883",
                    clientId);

    try {
        IMqttToken token = client.connect();
        token.setActionCallback(new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                // We are connected
                Log.d("mqtt", "connected, token:" + asyncActionToken.toString());
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                // Something went wrong e.g. connection timeout or firewall problems
                Log.d("mqtt", "not connected" + exception.toString());

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

Logcat:

 2020-06-18 14:49:52.969 6808-6858/com.example.testmqtt D/AlarmPingSender: 
                 Unregister alarmreceiver to MqttServicepaho8018389353240
 2020-06-18 14:50:10.537 6808-6830/com.example.testmqtt D/EGL_emulation: 
                 eglMakeCurrent: 0xb6585120: ver 2 0 (tinfo 0xb6583230)
 2020-06-18 14:50:12.213 6808-6830/com.example.testmqtt D/EGL_emulation: 
                 eglMakeCurrent: 0xb6585120: ver 2 0 (tinfo 0xb6583230)
 2020-06-18 14:50:23.022 6808-7040/com.example.testmqtt D/AlarmPingSender: 
                 Unregister alarmreceiver to MqttServicepaho8018389353240
 2020-06-18 14:50:23.037 6808-6808/com.example.testmqtt D/mqtt: not 
                 connectedMqttException (0) - 
                 java.net.SocketTimeoutException: connect timed out

来源:https://stackoverflow.com/questions/62428343/paho-mqtt-not-connecting-android-studio-to-the-mosquitto-broker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!