我从MQTT接收消息的代码不起作用?

时间:2016-05-01 13:50:26

标签: java android mqtt mosquitto

所以在我的Android App中,我编写了一些代码来订阅MQTT,并且(现在)只输出它收到的消息。但它不起作用,我不知道为什么,这是我的代码:

public class MainActivity extends AppCompatActivity implements MqttCallback{
    private MqttClient client;
    private String clientId = MqttClient.generateClientId();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button myButton = (Button) findViewById(R.id.button);


        if (myButton != null) {
            myButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    doDemo();
                }
            });
        }
    }

    public void doDemo() {
        try {
            client = new MqttClient("tcp://test.mosquitto.org:1883", clientId);
            client.connect();
            client.setCallback(this);
            client.subscribe("IoTLuxSpotifyHaiss");
            Toast.makeText(MainActivity.this, "Success!", Toast.LENGTH_LONG).show();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void connectionLost(Throwable cause) {
        // TODO Auto-generated method stub

    }

    @Override
    public void messageArrived(String topic, MqttMessage message)
            throws Exception {
        System.out.println(message);
    }

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

所以当我按下按钮时,我希望它订阅MQTT主题,然后我添加了一个toast来帮助我测试它。但它似乎不起作用,并且toast通知永远不会显示。任何人都能指出我哪里出错了?

感谢。

编辑:按下按钮时,这是logcat消息:

05-01 15:04:41.702 14861-14861/com.example.haiss.iotcounterpart W/System.err: MqttException (0)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence.open(MqttDefaultFilePersistence.java:80)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:286)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:167)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:224)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:136)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at com.example.haiss.iotcounterpart.MainActivity.doDemo(MainActivity.java:38)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at com.example.haiss.iotcounterpart.MainActivity$1.onClick(MainActivity.java:30)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at android.view.View.performClick(View.java:5204)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at android.view.View$PerformClick.run(View.java:21153)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at android.os.Looper.loop(Looper.java:148)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
05-01 15:04:41.703 14861-14861/com.example.haiss.iotcounterpart W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:1)

好的,所以甚至没有在主线程问题上进入网络......

错误是因为MQTT客户端代码尝试在本地存储上打开文件,以便在以高(1/2)QOS级别发布时存储消息。导致错误的原因有两个:

  1. 您可能未授予对应用程序的存储访问权限
  2. 您不太可能有权写入MQTT客户端选择的默认位置。
  3. 最简单的方法是使用内存存储(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)作为MQTT客户端。

    ...
    MemoryPersistence persistence = new MemoryPersistence();
    client = new MqttClient("tcp://test.mosquitto.org:1883", clientId);
    ...
    

    一旦你解决了这个问题,你可能会遇到在UI线程上做网络io的问题。为避免这种情况,建议您查看Android提供的AsyncTask代码