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

My code to receive messages from MQTT isn't working?

所以在我的 Android 应用程序中,我编写了一些代码来订阅 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 主题,我添加了一个祝酒词只是为了帮助我测试它。但是它似乎不起作用并且吐司通知从不显示。谁能指出我哪里出错了?

谢谢。

编辑:这是我按下按钮时的 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.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)

好的,所以在主线程问题上连网络都没有...

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

  1. 您可能没有授予应用程序存储访问权限
  2. 您不太可能有权写入 MQTT 客户端选择的默认位置。

解决这个问题的最简单方法是为 MQTT 客户端使用内存存储 (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)。

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

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