开发者社区 > 云原生 > 消息队列 > 正文

mqtt消息队列,Android接收消息

//点击按钮调用resavebutton() 方法,通过控制台发送消息,不能收到,程序也不报错,请大神指导,谢谢

private void resavebutton() {

//

    /**
     * 设置当前用户私有的MQTT的接入点。例如此处示意使用XXX,实际使用请替换用户自己的接入点。接入点的获取方法是,在控制台申请MQTT实例,每个实例都会分配一个接入点域名。
     */
    final String broker ="tcp://mqf-p5o4k36qb8.mqtt.aliyuncs.com:1883";
    /**
     * 设置阿里云的AccessKey,用于鉴权
     */
    final String acessKey ="LTAIEGaxwGajao9g";
    /**
     * 设置阿里云的SecretKey,用于鉴权
     */
    final String secretKey ="NN0Z8mBsOJDckEGiZNqiaVuoozw6lx";
    /**
     * 发消息使用的一级Topic,需要先在MQ控制台里申请
     */
    final String topic ="liangjing12345";

    /**
     * MQTT的ClientID,一般由2部分组成,GroupID@@@DeviceID
     * 其中GroupID在MQ控制台里申请
     * DeviceID由应用方设置,可能是设备编号等,需要唯一,否则服务端拒绝重复的ClientID连接
     */
    final String consumerClientId ="GID_LIANGJING@@@456";//消费者
   // final String producerClientId ="GID_XXX@@@@XXXXXX";
    String sign;
    MemoryPersistence persistence = new MemoryPersistence();
    try {
        final MqttClient sampleClient = new MqttClient(broker, consumerClientId, persistence);
        final MqttConnectOptions connOpts = new MqttConnectOptions();
        System.out.println("Connecting to broker: " + broker);
        /**
         * 计算签名,将签名作为MQTT的password。
         * 签名的计算方法,参考工具类MacSignature,第一个参数是ClientID的前半部分,即GroupID
         * 第二个参数阿里云的SecretKey
         */
        sign = MacSignature.macSignature(consumerClientId.split("@@@")[0], secretKey);
        /**
         * 设置订阅方订阅的Topic集合,此处遵循MQTT的订阅规则,可以是一级Topic,二级Topic,P2P消息不需要显式订阅,
         */
        final String[] topicFilters=new String[]{topic+"/notice/"};
        final int[]qos={0};
        //final String[] topicFilters=new String[]{topic+"/notice/",topic+"/p2p"};
        //final int[]qos={0,0};
        connOpts.setUserName(acessKey);
        connOpts.setServerURIs(new String[] { broker });
        connOpts.setPassword(sign.toCharArray());
        connOpts.setCleanSession(false);
        connOpts.setKeepAliveInterval(100);
        sampleClient.setCallback(new MqttCallback() {
            public void connectionLost(Throwable throwable) {
                System.out.println("mqtt connection lost");
                throwable.printStackTrace();
                while(!sampleClient.isConnected()){
                    try {
                        sampleClient.connect(connOpts);
                        sampleClient.subscribe(topicFilters,qos);
                    } catch (MqttException e) {
                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
                //System.out.println("messageArrived:" + topic + "------" + new String(mqttMessage.getPayload()));
                Toast.makeText(getApplication(),"接收到了消息",Toast.LENGTH_LONG).show();
            }
            public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
                //System.out.println("deliveryComplete:" + iMqttDeliveryToken.getMessageId());
                Toast.makeText(getApplication(),"接收到了消息",Toast.LENGTH_LONG).show();
            }
        });
        sampleClient.connect(connOpts);
        sampleClient.subscribe(topicFilters,qos);
        Thread.sleep(Integer.MAX_VALUE);
    } catch (Exception me) {
        me.printStackTrace();
    }

}

展开
收起
煮熟的土豆 2017-02-08 22:17:33 7026 0
2 条回答
写回答
取消 提交回答
  • 订阅的主题是不是少了一个/

    2019-07-17 20:46:37
    赞同 展开评论 打赏
  • 可以先看下 通过控制台发送消息的topic是否匹配,消息是否能在控制台查到

    2019-07-17 20:46:37
    赞同 展开评论 打赏

多个子产品线联合打造金融级高可用消息服务以及对物联网的原生支持,覆盖多行业。

相关产品

  • 云消息队列 MQ
  • 相关电子书

    更多
    RocketMQ Client-GO 介绍 立即下载
    RocketMQ Prometheus Exporter 打造定制化 DevOps 平台 立即下载
    基于 RocketMQ Prometheus Exporter 打造定制化 DevOps 平台 立即下载