发布包含多个String的消息,Pubnub& Android的

时间:2016-11-03 01:32:06

标签: android json string publish pubnub

这是我通过PubNub频道发送带有一个字符串的消息的方式:

pubnub.publish().channel("my_channel").message("hello").async(new
PNCallback<PNPublishResult>() {
    // ...
});

这是我在连接到频道的客户端中收到消息的方式:

@Override
public void message(PubNub pubnub, final PNMessageResult message) {
    if (message.getChannel() != null) {
        final JsonNode received = message.getMessage();
        String received_str = received.asText();
    }
    // ...
}

如果我只发送一个包含该消息的字符串,这可以正常工作,但如果我想发送和接收多个消息,我该怎么做?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。

发送时间:

pubnub.publish().channel("my_channel").message(Arrays.asList
("string a", "string b", "string c", "string d")).async(new
PNCallback<PNPublishResult>() {
    // ...
});

这是接收:

@Override
public void message(PubNub pubnub, final PNMessageResult message) {
    if (message.getChannel() != null) {
        final JsonNode received = message.getMessage();
        String string_a = received.get(0).asText();
        String string_b = received.get(1).asText();
        String string_c = received.get(2).asText();
        String string_d = received.get(3).asText();
    }
    // ...
}