Android的 - 用于发布泛美卫生组织MQTT服务 [英] android - Paho MQTT service for publishing

查看:703
本文介绍了Android的 - 用于发布泛美卫生组织MQTT服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid和服务。我的目标是能够建立的订阅和做主题字符串出版物。主题字符串,客户端ID是建立解析文本字段输入后。我现在用的是泛美卫生组织MQTT服务(下载源和生成JAR)。

I am new to Android and services. My aim is to be able to set-up subscriptions and do publications on topic strings. The topic strings and client ID are set-up after parsing input of text fields. I am using the Paho MQTT service (downloaded the source and built the JAR).

以下原因一个空指针异常的 c.publish()。该的logcat 显示了异常的在 IMqttDeliveryToken发布(字符串话题,MqttMessage消息,对象的UserContext,IMqttActionListener回调)方法 MqttAndroidClient ,其中一个传递令牌上当受骗。

The following causes a Null Pointer Exception at c.publish(). The logcat shows the exception at the IMqttDeliveryToken publish(String topic, MqttMessage message, Object userContext, IMqttActionListener callback) method in MqttAndroidClient where a delivery token is being taken.

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set locale;
        l = getResources().getConfiguration().locale;
    }

    @Override
    protected void onResume() {
        super.onResume();
        addButtonListener();        
    }

    private void addButtonListener() {
        Button submitButton = (Button) findViewById(R.id.buttonSubmit);

        submitButton.setOnClickListener(new OnClickListener() {
// ...
// validation code for fields in layout
// ...
// Finally, this.

                    MemoryPersistence mPer = new MemoryPersistence();
                    String clientId = UUID.randomUUID().toString();
                    String brokerUrl = "tcp://m2m.eclipse.org:1883";
                    MqttAndroidClient c = new MqttAndroidClient(getApplicationContext(), brokerUrl, clientId, mPer);
                    try {
                        c.connect(); 
                        String topic = "transfers/topic";
                        String msg = "topic payload"
                        MqttMessage m = new MqttMessage();
                        m.setPayload(msg.getBytes());
                        m.setQos(2);
                        m.setRetained(false);
                        c.publish(topic, m); 
                    } catch (MqttException e) {
                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

能否请你告诉我如何使用该服务来发布和订阅?我也浏览(泛美卫生组织安卓)示例项目。该LWT和发布似乎要合并的布局LWT( activity_publish.xml )似乎是用于出版及。

Can you please tell me how to use the service to publish and subscribe ? I did browse through the sample project (from Paho Android). The LWT and publish seems to be merged as the layout for LWT (activity_publish.xml) seems to be used for publication as well.

推荐答案

该NullPointerException异常是因为connect()的调用异步方法,你需要实现一个ActionListener。 在成功的情况下,你可以发送邮件。

The NullPointerException is because connect() calls an asynchronous method and you need to implement an ActionListener. In case of success you could send messages.

        Log.i(LOGTAG, "MQTT Start");
        MemoryPersistence memPer = new MemoryPersistence();
        final MqttAndroidClient client = new MqttAndroidClient(context, "tcp://192.168.0.13:1883", username, memPer);

        try {
            client.connect(null, new IMqttActionListener() {

                @Override
                public void onSuccess(IMqttToken mqttToken) {
                    Log.i(LOGTAG, "Client connected");
                    Log.i(LOGTAG, "Topics="+mqttToken.getTopics());

                    MqttMessage message = new MqttMessage("Hello, I am Android Mqtt Client.".getBytes());
                    message.setQos(2);
                    message.setRetained(false);

                    try {
                        client.publish("messages", message);

                        Log.i(LOGTAG, "Message published");

                        client.disconnect();
                        Log.i(LOGTAG, "client disconnected");
                    } catch (MqttPersistenceException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (MqttException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }



                }

                @Override
                public void onFailure(IMqttToken arg0, Throwable arg1) {
                    // TODO Auto-generated method stub
                    Log.i(LOGTAG, "Client connection failed: "+arg1.getMessage());

                }
            });

这篇关于Android的 - 用于发布泛美卫生组织MQTT服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆