I’m having some problems using the fusesource mqtt-client-java1.4-uber-1.0 (in an android app). when I provide a wrong IP or the mqtt broker is not running on the proper IP it alerts the message “Could not connect (callback)” but not “Could not connect (listener)”. When i provide the proper ip and the broker is running no alerts appear at all. I Guess the listeners do not work at all and somehow the success callback of the connection doesn’t eighter. any idears? Below the full code of the android activity.
I’m using this version of the mqtt-client: mqtt-client-java1.4-uber-1.0.jar
I also tried different messe brokers (servers) which are RSMB and Mosquitto
package racenet.mqtt;
import java.net.URISyntaxException;
import org.fusesource.hawtbuf.Buffer;
import org.fusesource.hawtbuf.UTF8Buffer;
import org.fusesource.mqtt.client.Callback;
import org.fusesource.mqtt.client.CallbackConnection;
import org.fusesource.mqtt.client.Listener;
import org.fusesource.mqtt.client.MQTT;
import racenet.mqtt.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
public class MQTTActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MQTT mqtt = new MQTT();
try {
mqtt.setHost("tcp://proper-ip-here:1883");
} catch (URISyntaxException e) {
new AlertDialog.Builder(MQTTActivity.this)
.setMessage("Could not set host")
.setNeutralButton("OK", null)
.show();
}
final CallbackConnection connection = mqtt.callbackConnection();
connection.listener(new Listener() {
public void onConnected() {
new AlertDialog.Builder(MQTTActivity.this)
.setMessage("Connected (listener)")
.setNeutralButton("OK", null)
.show();
}
public void onDisconnected() {
new AlertDialog.Builder(MQTTActivity.this)
.setMessage("Disconnected (listener)")
.setNeutralButton("OK", null)
.show();
}
public void onFailure(Throwable arg0) {
new AlertDialog.Builder(MQTTActivity.this)
.setMessage("Failure (listener)")
.setNeutralButton("OK", null)
.show();
}
public void onPublish(UTF8Buffer arg0, Buffer arg1, Runnable arg2) {
new AlertDialog.Builder(MQTTActivity.this)
.setMessage("Publish (listener)")
.setNeutralButton("OK", null)
.show();
}
});
connection.connect(new Callback<Void>() {
public void onFailure(Throwable value) {
new AlertDialog.Builder(MQTTActivity.this)
.setMessage("Could not connect (callback)")
.setNeutralButton("OK", null)
.show();
}
public void onSuccess(Void v) {
new AlertDialog.Builder(MQTTActivity.this)
.setMessage("Connected (callback)")
.setNeutralButton("OK", null)
.show();
}
});
}
}
mqtt-client-java1.4-uber-1.0 simply is a crappy implementation