试图将mqtt消息显示为吐司

时间:2016-05-03 13:28:49

标签: android mqtt paho

您好我正试图从mqtt经纪人那里得到消息,但是我一直收到Toast的错误(代码中的红色下划线),有人可以告诉我如何修改它吗?

这是代码

   import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;



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

public class MainActivity extends AppCompatActivity implements MqttCallback{
    MqttClient client;
    private String clientInfo = MqttClient.generateClientId();
    //private final MqttMessage message = new MqttMessage();


    public void findSpace(View view) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //adapted from https://www.eclipse.org/paho/files/javadoc/org/eclipse/paho/client/mqttv3/persist/MemoryPersistence.html
        try {
            MemoryPersistence mp = new MemoryPersistence();
            client = new MqttClient("tcp://10.150.37.228:1883", clientInfo, mp);
            MqttConnectOptions mqttConnectOps = new MqttConnectOptions();
            mqttConnectOps .setCleanSession(true);
            client.connect(mqttConnectOps );
            client.setCallback(this);
            client.subscribe("test");
        } catch (MqttException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void connectionLost(Throwable cause) {

    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        System.out.println(message);
        CharSequence cs = new String(message.getPayload());
        Toast.makeText(getApplicationContext(), cs, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {

    }
}

3 个答案:

答案 0 :(得分:1)

试试这个:

  

Toast.makeText(getApplicationContext(),message.getPayload(),Toast.LENGTH_SHORT).show();

答案 1 :(得分:0)

message.getPayload()是一个字节数组文档https://www.eclipse.org/paho/files/javadoc/org/eclipse/paho/client/mqttv3/MqttMessage.html#getPayload()

你应该这样做

如果您正在参加活动

Toast.makeText(MainActivity.this,new String(message.getPayload()),Toast.LENGTH_SHORT).show();

希望有所帮助:)

答案 2 :(得分:0)

不要使用RunOnUithread。您应该使用一个Handler,它可以在主线程和其他线程之间异步通信。

无论如何,这不起作用。

Toast.makeText(this,new String(message.getPayload()),Toast.LENGTH_SHORT).show();

"这"是可运行的对象而不是活动上下文。所以你应该传递上下文而不是"这个"。而且你必须传递一个CharSequence而不是一个字符串。这可能有效:

CharSequence cs = new String(message.getPayload());
Toast.makeText(getApplicationContext(), cs, Toast.LENGTH_SHORT).show();

编辑: 感谢Spurdow提示您使用AppCompatActivity而不是" getApplicationContext()"应该使用以下内容。

CharSequence cs = new String(message.getPayload());
Toast.makeText(MainActivity.this, cs, Toast.LENGTH_SHORT).show();