我在检查频道名称时总是出错,如
mChatClient.getChannels().getChannel("one", new CallbackListener<Channel>() {
@Override
public void onSuccess(Channel channel) {
if (channel != null) {
joinChannel(channel);
} else {
mChatClient.getChannels().createChannel("one",
Channel.ChannelType.PUBLIC, new CallbackListener<Channel>() {
@Override
public void onSuccess(Channel channel) {
if (channel != null) {
joinChannel(channel);
}
}
@Override
public void onError(ErrorInfo errorInfo) {
Log.e(TAG,"Error creating channel: " + errorInfo.getErrorText());
}
});
}
}
@Override
public void onError(ErrorInfo errorInfo) {
Log.e(TAG,"Error retrieving channel: " + errorInfo.getErrorText());
}
});
这个总是只会出现错误块。我可以知道出了什么问题吗?
答案 0 :(得分:2)
你在做的是:
如果您查看代码,则表明您正在使用onSuccess
进行创建,但是,如果找不到您的频道,则永远不会触发onSuccess
,因此不会尝试创建频道。
您应该做的是onError
创建频道。 (未经测试,可能是一些错误):
mChatClient.getChannels().getChannel("one", new CallbackListener<Channel>() {
@Override
public void onSuccess(Channel channel) {
joinChannel(channel);
}
@Override
public void onError(ErrorInfo errorInfo) {
Log.e(TAG,"Error retrieving channel: " + errorInfo.getErrorText());
mChatClient.getChannels().createChannel("one", Channel.ChannelType.PUBLIC, new CallbackListener<Channel>() {
@Override
public void onSuccess(Channel channel) {
if (channel != null) {
joinChannel(channel);
}
}
@Override
public void onError(ErrorInfo errorInfo) {
Log.e(TAG,"Error creating channel: " + errorInfo.getErrorText());
}
});
}
});
答案 1 :(得分:1)
如前所述,我们需要在onerror()中创建频道,但这会导致每次都创建新频道。 每次创建新频道的原因是因为我们在getchannel()方法中与唯一名称进行比较;而我们在createChannel()方法中设置的是友好名称。尝试在成功时设置频道的单一名称。或者你可以试试这段代码:
mChatClient.getChannels().getChannel("general", new CallbackListener<Channel>() {
@Override
public void onSuccess(Channel channel) {
if (channel != null) {
joinChannel(channel);
}
}
@Override
public void onError(ErrorInfo errorInfo) {
Log.e(TAG,"Error retrieving channel: " + errorInfo.getMessage());
mChatClient.getChannels().channelBuilder().withUniqueName("general").withType(Channel.ChannelType.PUBLIC).build(new CallbackListener<Channel>() {
@Override
public void onSuccess(Channel channel) {
if (channel != null) {
joinChannel(channel);
}
}
@Override
public void onError(ErrorInfo errorInfo) {
Log.e(TAG,"Error creating channel: " + errorInfo.getMessage());
}
});
}
});