问题
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