我在使用fusesource mqtt-client-java1.4-uber-1.0(在android應用程序中)時遇到了一些問題。當我提供錯誤的IP或者mqtt代理沒有運行在正確的IP上時,它會提示消息「無法連接(回調)」,但不會「無法連接(監聽)」。當我提供正確的IP並且代理運行時,根本沒有警報出現。我猜聽衆根本不工作,不知何故,連接的成功回調不會更加激烈。任何想法?在android活動的完整代碼下方。android 2.1中的mqtt-client應用程序
我使用這個版本的MQTT客戶端的:mqtt-client-java1.4-uber-1.0.jar 我也嘗試過不同的展覽經紀人(服務器),這是RSMB和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();
}
});
}
}
好,我現在切換到MeQanTT,它更簡單,適合我的需求,並且按照它的原樣工作;) – 2012-03-17 09:37:44
如果已經提到了其他Android客戶端,那麼您可能想知道,在本月末應該有一個新的固定開源MQTT Java客戶端,它應該也能夠在android上工作。 - http://www.eclipse.org/paho/ – Dror 2012-03-19 10:42:18