我在我的项目(简单的聊天应用程序)的TextViews (消息)的ListView。下面是发布消息的代码:
public void publish(View view) {
final EditText mMessage = (EditText) MainActivity.this.findViewById(R.id.new_message);
final Map<String, String> message = ImmutableMap.<String, String>of("sender", MainActivity.this.nickName, "message", mMessage.getText().toString(), "timestamp", DateTimeUtil.getTimeStampUtc());
MainActivity.this.mPubnub_DataStream.publish().channel(Constants.CHANNEL_NAME).shouldStore(true).message(message).async(
new PNCallback<PNPublishResult>() {
@Override
public void onResponse(PNPublishResult result, PNStatus status) {
try {
if (!status.isError()) {
mMessage.setText("");
Log.v(TAG, "publish(" + JsonUtil.asJson(result) + ")");
} else {
Log.v(TAG, "publishErr(" + JsonUtil.asJson(status) + ")");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
);
}我正在尝试从历史记录中输出消息:
this.mPubnub_DataStream.history()
.channel(Constants.CHANNEL_NAME)
.async(new PNCallback<PNHistoryResult>() {
@Override
public void onResponse(PNHistoryResult result, PNStatus status) {
}
});我不知道如何重写onResponse方法。请帮帮我。顺便说一句,我可以检查我的消息是否存储了,但输出到终端并不是我真正需要的。
pubNub.history()
.channel(channelName)
.count(100)
.async(new PNCallback<PNHistoryResult>() {
@Override
public void onResponse(PNHistoryResult result, PNStatus status) {
for (int i = 0; i < 100; i++) {
System.out.println(result.getMessages().get(i).getEntry());
}
}
});发布于 2018-03-16 09:20:11
这更像是一个UI数据绑定问题,而不是一个PubNub问题,只要您实际从历史调用中接收到消息。
如果是这样的话,这个示例应用程序中的示例代码可能会有所帮助:github.com/pubnub/webinar android-intro
https://stackoverflow.com/questions/46344332
复制相似问题