Android上的MQTT:连接断开 [英] MQTT on Android: connection lost

查看:1370
本文介绍了Android上的MQTT:连接断开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android应用程序上使用MQTT客户端,但无法使其正常工作.
我的MQTT客户端是0.4.0,我的Android是4.0.3.

I'm trying to use an MQTT client on Android application but I cannot get it working.
My MQTT client is 0.4.0 and my Android is 4.0.3.

该应用程序非常简单:它具有一个EditText和一个用于发布文本的按钮.此外,它具有TextView来显示收到的消息.

The app is very simple: it has an EditText and a button to publish the text. Further it has a TextView to show the received message.

每次单击发布"按钮,都会显示连接丢失"消息,有时会显示两次或三次,因为它尝试多次连接然后断开连接.
同样,如果不单击按钮,如果我尝试通过另一个客户端在"123456789"主题上发布消息,则什么也不会发生(没有显示消息到达"消息).

Every time is click on the "Publish" button, the "connection lost" message is shown, sometimes two or three times, as it tried to connect multiple times and then disconnected.
Also, without clicking on the button, if I try to publish a message on the "123456789" topic via another client nothing happen (no "message arrived" message is shown).

我同时具有INTERNET和WRITE_EXTERNAL_STORAGE权限,并且该应用程序没有异常关闭,只是没有消息被发送/接收.

I put both the INTERNET and WRITE_EXTERNAL_STORAGE permissions and the application does not close with an exception, simply no message are sent/received.

这是MainActivity

This is the MainActivity

package com.example.mqtttest;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    private static final String MQTT_DIR = "/mnt/sdcard";
    private static final String MQTT_URI = "tcp://m2m.eclipse.org:1883";
    private MqttClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            MqttDefaultFilePersistence mdfp = new MqttDefaultFilePersistence(
                    MQTT_DIR);
            client = new MqttClient(MQTT_URI, "1", mdfp);

            client.connect();
            client.subscribe("123456789");
            client.setCallback(new MqttCallback() {

                @Override
                public void connectionLost(Throwable arg0) {
                    // TODO Auto-generated method stub
                    System.out.println("Connection lost");
                    try {
                        client.connect();
                    } catch (MqttException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void messageArrived(String arg0, MqttMessage arg1)
                        throws Exception {
                    // TODO Auto-generated method stub
                    TextView txt = (TextView) findViewById(R.id.txt);
                    txt.setText(arg1.toString());
                }
            });

            Button btn = (Button) findViewById(R.id.btn);
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    try {
                        MqttMessage msg = new MqttMessage();
                        msg.setPayload((((EditText) findViewById(R.id.editTxt))
                                .getText().toString()).getBytes());
                        client.publish("123456789", msg);
                    } catch (MqttException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        } catch (MqttException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


新信息 如果我使用org.eclipse.paho.client.mqttv3.jar,则该应用程序可以正常运行.
问题仅在于org.eclipse.paho.client.mqttv3-0.9.0.jar


New info If I use org.eclipse.paho.client.mqttv3.jar the app works fine.
The problem is only with org.eclipse.paho.client.mqttv3-0.9.0.jar

推荐答案

您的经纪人可能正在使用该规范的3.1.1版本.

Your Broker could be using the version 3.1.1 of the spec.

Paho 1.0实现了MQTT 3.1.1以及现有的MQTT 3.1规范. Paho 0.9仅实现了MQTT 3.1规范.

Paho 1.0 implements MQTT 3.1.1 as well as the existing MQTT 3.1 specification. While Paho 0.9 implemented just only MQTT 3.1 spec.

这篇关于Android上的MQTT:连接断开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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