android 2.1 app中的mqtt-client

时间:2012-03-16 16:59:52

标签: android mqtt

我在使用fusesource mqtt-client-java1.4-uber-1.0(在Android应用程序中)时遇到了一些问题。当我提供错误的IP或mqtt代理没有在正确的IP上运行时,它会警告消息“无法连接(回调)”但不能“无法连接(监听器)”。当我提供正确的IP并且代理正在运行时,根本不会出现警报。我猜测听众根本不工作,不知何故,连接的成功回调并不明智。任何想法?下面是android活动的完整代码。

我正在使用此版本的mqtt-client:mqtt-client-java1.4-uber-1.0.jar 我还尝试了不同的messe代理(服务器)RSMBMosquitto

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();
            }
        });
    }
}

2 个答案:

答案 0 :(得分:5)

现在有一个IBM提供的MQTT客户端Java实现作为Eclipse的开源代码 - http://git.eclipse.org/c/paho/

答案 1 :(得分:4)

mqtt-client-java1.4-uber-1.0只是一个糟糕的实现