使用Telegram JNI Lib。想要调用getChatList()方法并等待TdApi.GetChats()返回结果。
该方法的当前实现如下:
发送请求以获取Telegram JNI Lib中的聊天记录:
client.send(new TdApi.GetChats (offsetOrder, offsetChatId, limit - chatList.size ()), new Client.ResultHandler()
完成后,递归调用getChatList()方法并打印结果。
如何调用getChatList()方法并等待它执行?
private static void getChatList(final int limit) {
synchronized (chatList) {
if (!haveFullChatList && limit > chatList.size()) {
// have enough chats in the chat list or chat list is too small
long offsetOrder = Long.MAX_VALUE;
long offsetChatId = 0;
if (!chatList.isEmpty()) {
OrderedChat last = chatList.last();
offsetOrder = last.order;
offsetChatId = last.chatId;
}
client.send(new TdApi.GetChats(offsetOrder, offsetChatId, limit - chatList.size()), new Client.ResultHandler() {
@Override
public void onResult(TdApi.Object object) {
switch (object.getConstructor()) {
case TdApi.Error.CONSTRUCTOR:
System.err.println("Receive an error for GetChats:" + newLine + object);
break;
case TdApi.Chats.CONSTRUCTOR:
long[] chatIds = ((TdApi.Chats) object).chatIds;
if (chatIds.length == 0) {
synchronized (chatList) {
haveFullChatList = true;
}
}
// chats had already been received through updates, let's retry request
getChatList(limit);
break;
default:
System.err.println("Receive wrong response from TDLib:" + newLine + object);
}
}
});
return;
}
// have enough chats in the chat list to answer request
java.util.Iterator<OrderedChat> iter = chatList.iterator();
System.out.println();
System.out.println("First " + limit + " chat(s) out of " + chatList.size() + " known chat(s):");
for (int i = 0; i < limit; i++) {
long chatId = iter.next().chatId;
TdApi.Chat chat = chats.get(chatId);
synchronized (chat) {
System.out.println(chatId + ": " + chat.title);
}
}
}
}