关键词:
【中文标题】从 Java 中的 Google Talk 中检索离线消息和聊天历史记录【英文标题】:Retrieve Offline Messages and Chat History From Google Talk in Java 【发布时间】:2014-08-29 07:00:54 【问题描述】:我必须从 Google Talk 中检索与我有过对话的特定客户(朋友/XMPP 客户)的离线消息。
另外,我想找回聊天记录。
现在,我正在使用 Hash Map 来跟踪对话并且无法获得离线消息。
我已与“talk.google.com”建立了 XMPP 连接。我目前能够通过我的控制台向客户端发送消息。我已经实现了消息侦听器接口,因此可以在我的控制台本身中接收消息。
我有另一个实现(使用 OfflineMessageManager),我尝试提取离线消息头作为开始,不幸的是,它失败并出现 Null 异常。
我使用过 smack-tcp 版本 4.0.3 和 smackx 3.1.0。
请在下面找到我到目前为止尝试过的内容...
工作实施:
公共类 StartChatImpl 实现 startChat、MessageListener
static XMPPConnection myXMPPConnection;
static ArrayList<String> myFriends = new ArrayList<String>();
ArrayList<Msg> messages;
HashMap<String,ArrayList> conversation = new HashMap<String, ArrayList>();
OfflineMessageManager offlineMessages;
@Override
public void engageChat(ChatRequest chatRequest) throws XMPPException
ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
//config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setSendPresence(true);
myXMPPConnection = new XMPPTCPConnection(config);
try
System.out.println("Connecting to talk.google.com");
myXMPPConnection.connect();
System.out.println("Logging you in...");
myXMPPConnection.login(chatRequest.getUsername(),chatRequest.getPassword());
Presence pres = new Presence(Presence.Type.unavailable);
myXMPPConnection.sendPacket(pres);
offlineMessages = new OfflineMessageManager(myXMPPConnection);
System.out.println("You have been successfully connected.");
catch (SmackException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
public void send (String message, String to) throws XMPPException
Chat chat = ChatManager.getInstanceFor(myXMPPConnection).createChat(to, this);
//System.out.println("********************************************");
//System.out.println("CHAT ID: "+ chat.getThreadID());
//System.out.println("Participant: "+chat.getParticipant());
//System.out.println("********************************************");
try
chat.sendMessage(message);
String friendName = myXMPPConnection.getRoster().getEntry(chat.getParticipant()).getName();
if(conversation.containsKey(friendName))
messages = conversation.get(friendName);
Calendar calendar = Calendar.getInstance();
java.util.Date now = calendar.getTime();
java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());
message = "bot says: "+message;
Msg actualMsg = new Msg(currentTimestamp,message,"bot");
messages.add(actualMsg);
//messages.add("bot says: "+message);
else
messages = new ArrayList<Msg>();
//messages.add("Bot initiated the conversation on: "+new Date());
//messages.add("bot says: "+message);
Calendar calendar = Calendar.getInstance();
java.util.Date now = calendar.getTime();
java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());
message = "bot says: "+message;
Msg actualMsg = new Msg(currentTimestamp,message,"bot");
messages.add(actualMsg);
conversation.put(friendName,messages);
catch (SmackException.NotConnectedException e)
e.printStackTrace();
System.out.println("You said "+"'"+message+"'"+" to "+myXMPPConnection.getRoster().getEntry(chat.getParticipant()).getName());
PacketListener myPacketListener = new PacketListener()
@Override
public void processPacket(Packet p)
if (p instanceof Message)
Message msg = (Message) p;
;
public void displayMyFriends()
Roster roster = myXMPPConnection.getRoster();
Collection entries = roster.getEntries();
System.out.println("You currently have: "+entries.size()+" friends available to chat.");
int counter = 0;
Iterator i = entries.iterator();
while(i.hasNext())
RosterEntry r = (RosterEntry) i.next();
Presence.Type entryPresence;
entryPresence = roster.getPresence(r.getUser()).getType();
System.out.println((counter+1)+" . "+r.getName() +" is "+entryPresence);
//System.out.println("id:..."+r.getUser());
myFriends.add(r.getUser());
counter++;
public void disconnectMe()
try
System.out.println("Disconnection in progress...");
myXMPPConnection.disconnect();
System.out.println("You have been successfully disconnected.");
catch (SmackException.NotConnectedException e)
e.printStackTrace();
@Override
public void processMessage(Chat chat, Message message)
if((message.getType() == Message.Type.chat) && (message.getBody()!= null))
System.out.println(myXMPPConnection.getRoster().getEntry(chat.getParticipant()).getName() + " says: " + message.getBody());
String myMsg = myXMPPConnection.getRoster().getEntry(chat.getParticipant()).getName() + " says: " + message.getBody();
Calendar calendar = Calendar.getInstance();
java.util.Date now = calendar.getTime();
java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());
Msg actualMsg = new Msg(currentTimestamp,myMsg,"customer");
conversation.get(myXMPPConnection.getRoster().getEntry(chat.getParticipant()).getName()).
add(actualMsg);
public void receiveMessage()
myXMPPConnection.addPacketListener(myPacketListener, null);
public void retrieveUnservicedMessagesForSpecificPerson(String clientName)
ArrayList<Msg> myMessages = conversation.get(clientName);
Calendar calendar = Calendar.getInstance();
java.util.Date now = calendar.getTime();
java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());
System.out.println("Unserviced messages from: "+ clientName);
if(!myMessages.isEmpty())
int counter = myMessages.size()-1;
System.out.println("Size of array list: "+counter);
boolean found = false;
java.sql.Timestamp lastBotTimestamp = null;
while (counter != 0 && found == false)
if(myMessages.get(counter).getOwner()=="bot")
lastBotTimestamp = myMessages.get(counter).getTimestamp();
found = true;
else
counter = counter-1;
for(Msg msg:myMessages)
if(msg.getTimestamp().before(currentTimestamp) &&
msg.getTimestamp().after(lastBotTimestamp))
System.out.println("---------------------------");
System.out.println("-"+msg.getActualMessage());
System.out.println("-"+msg.getTimestamp());
System.out.println("---------------------------");
public void returnConversation(String name)
System.out.println("Name of participant entered: "+ name);
System.out.println("Conversation history is: ");
ArrayList<Msg> m = conversation.get(name);
for(Msg msg:m)
System.out.println(msg.getActualMessage());
System.out.println("on: "+msg.getTimestamp());
public void returnAllUnservicedMessagesHistory()
for(String name: conversation.keySet())
System.out.println("History of unserviced messages for: "+ name);
retrieveUnservicedMessagesForSpecificPerson(name);
public void getOfflineMessagesCount()
try
System.out.println("Number of offline messages: "+ offlineMessages.getMessageCount());
catch (XMPPException e)
e.printStackTrace();
我的主类实现:
公共类 MainGoogleChatApp
public static void main(String[] args)
//Step 1: need to make a chat request with your username and password.
Scanner input = new Scanner(System.in);
System.out.println("Please enter your username to start: ");
String myUsername = input.next();
System.out.println("Please enter your password to continue: ");
String myPassword = input.next();
ChatRequest myChatRequest = new ChatRequest();
myChatRequest.setUsername(myUsername);
myChatRequest.setPassword(myPassword);
//Step 2: Need to initiate a connection to talk.google.com
StartChatImpl convo = new StartChatImpl();
try
convo.engageChat(myChatRequest);
catch (XMPPException e)
e.printStackTrace();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String myMessage;
System.out.println("Friend list.");
convo.displayMyFriends();
convo.receiveMessage();
try
while(true)
System.out.println("Input Which friend you want to chat with '0 to sign out' : ");
int num = input.nextInt() -1;
if (num == -1)
break;
String chatWith = StartChatImpl.myFriends.get(num);
System.out.print("Type your message: ");
myMessage=br.readLine();
try
convo.send(myMessage,chatWith);
catch (XMPPException e)
e.printStackTrace();
convo.displayMyFriends();
catch (IOException e)
e.printStackTrace();
System.out.println("Enter participant name to get specific chat...");
String yourName = null;
try
yourName = br.readLine();
catch (IOException e)
e.printStackTrace();
System.out.println("/./././././././././././././..//.");
if(yourName!=null)
System.out.println("Your name is not null...ok");
convo.returnConversation(yourName);
System.out.println("Enter another participant's name to get specific chat...");
String yourName1 = null;
try
yourName1 = br.readLine();
catch (IOException e)
e.printStackTrace();
System.out.println("/./././././././././././././..//.");
if(yourName1!=null)
System.out.println("Your name is not null...ok");
convo.returnConversation(yourName1);
System.out.println("Select a client name for whom you want to view unserviced messages: ");
String yourClientName = null;
try
yourClientName = br.readLine();
catch (IOException e)
e.printStackTrace();
if(yourClientName!=null)
System.out.println("...........................................");
convo.retrieveUnservicedMessagesForSpecificPerson(yourClientName);
System.out.println("...........................................");
convo.returnAllUnservicedMessagesHistory();
System.out.println("You have chosen to disconnect yourself from talk.google.com");
convo.disconnectMe();
System.exit(0);
尝试检索离线消息标头:
公共类 TestOfflineService
XMPPConnection xmppConnection = null;
//OfflineMessageManager offlineMessageManager = null;
public void makeConnection()
ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
//config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setSendPresence(true);
xmppConnection = new XMPPTCPConnection(config);
try
System.out.println("Connecting to talk.google.com");
xmppConnection.connect();
System.out.println("Logging you in...");
xmppConnection.login("*******.*@***.com", "*****");
System.out.println("You have been successfully connected.");
catch (SmackException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
catch (XMPPException e)
e.printStackTrace();
public void makeMeUnavailable()
try
xmppConnection.sendPacket(new Presence(Presence.Type.unavailable));
catch (SmackException.NotConnectedException e)
e.printStackTrace();
public void getOfflineMessages()
OfflineMessageManager offlineMessageManager1 = new OfflineMessageManager(xmppConnection);
try
Iterator<OfflineMessageHeader> headers = offlineMessageManager1.getHeaders();
if(headers.hasNext())
System.out.println("Messages found");
catch (XMPPException e)
e.printStackTrace();
public static void main(String[] args)
TestOfflineService test = new TestOfflineService();
test.makeConnection();
test.makeMeUnavailable();
System.out.println("Enter messages");
try
Thread.sleep(1000);
catch (InterruptedException e)
e.printStackTrace();
System.out.println("Getting offline messages.....");
test.getOfflineMessages();
【问题讨论】:
【参考方案1】:OfflineMessageManager
尝试使用XEP-0013 检索离线消息。这个 XEP 不是由 GTalk 实现的。另请注意,启动环聊意味着您将永远不会再收到离线消息,因为环聊实例永远不会离线。
GTalk 也没有用于检索聊天记录的 XMPP API,例如 XEP-0136 或 XEP-0313。
【讨论】:
如何检索 Google Talk 用户状态消息
】如何检索GoogleTalk用户状态消息【英文标题】:HowcanIretrieveaGoogleTalkusersStatusMessage【发布时间】:2010-09-3014:36:59【问题描述】:我希望能够使用Python检索用户的GoogleTalk状态消息,很难找到有关如何使用其中一些库的文档。【问... 查看详情
GWT - 从 GWT 应用程序连接到 talk.google.com
】GWT-从GWT应用程序连接到talk.google.com【英文标题】:GWT-Connecttotalk.google.comfromGWTApplication【发布时间】:2013-01-2506:24:24【问题描述】:我想从我的GWT应用程序连接到谷歌服务器,以将谷歌聊天集成到我的应用程序中。我曾尝试使... 查看详情
java示例代码_我从URL中检索图像,并将其存储为Java(google app engine)中的Blob
java示例代码_我从URL中检索图像,并将其存储为Java(google app engine)中的Blob 查看详情
从 Google App Engine 设置 Google Talk 状态(带身份验证)
】从GoogleAppEngine设置GoogleTalk状态(带身份验证)【英文标题】:SettingGoogleTalkstatus(withauthentication)fromGoogleAppEngine【发布时间】:2012-09-0903:09:22【问题描述】:我正在尝试构建一个站点,该站点使用内置的XMPPPythonAPI(或第三方,... 查看详情
无法从 Google Drive API 检索 thumbnailLink
...描述】:我正在尝试使用python和googleDriveAPI从共享驱动器中的文件中获取thumbnailLink,但是,文件信息不包括thumbnailLink(尽管对于大多数文件,hasThumbnail字段 查看详情
java从java中的array中检索值(代码片段)
Python 中的 Google Talk/XMPP 音频支持
】Python中的GoogleTalk/XMPP音频支持【英文标题】:GoogleTalk/XMPPaudiosupportinPython【发布时间】:2012-07-2619:43:16【问题描述】:我正在寻找一个Python库来与可以处理音频聊天的GoogleTalk进行通信。有很多只能做文本,但我找不到任何支持... 查看详情
从 Android 以编程方式更改 Google Talk 在线状态
】从Android以编程方式更改GoogleTalk在线状态【英文标题】:ChangeGoogleTalkonlinestatusprogrammaticallyfromAndroid【发布时间】:2011-02-0717:43:39【问题描述】:有谁知道如何通过从另一个应用程序调用用户来更改用户的在线状态?理想情况下... 查看详情
从java中的对象表中检索结构类型
】从java中的对象表中检索结构类型【英文标题】:Retrievestructtypefromaobjecttableinjava【发布时间】:2013-06-2413:59:12【问题描述】:你好,我需要从oracle对象表(地址)中获取值,为此我想用oracle类型(地址)映射一个java类(地址)... 查看详情
如何使用 smack API 从 Google Talk 获取我的个人资料图片?
】如何使用smackAPI从GoogleTalk获取我的个人资料图片?【英文标题】:HowcanIobtainmyprofilepicturefromGoogleTalkusingsmackAPI?【发布时间】:2012-08-0207:51:34【问题描述】:如何使用SmackAPI或任何其他API从我的GoogleTalk个人资料中获取我的个人资... 查看详情
如何从java中的mysql表中检索数组中的值? [关闭]
】如何从java中的mysql表中检索数组中的值?[关闭]【英文标题】:howtoretrievevalueinarrayfrommysqltableinjava?[closed]【发布时间】:2014-01-2307:53:16【问题描述】:我想使用java将字符串值存储在数组或单独的变量中,请缩短我的问题谢谢Str... 查看详情
我们可以从 Android 中的 Google Drive API 检索数据吗?
】我们可以从Android中的GoogleDriveAPI检索数据吗?【英文标题】:CanweretrievedatabackfromGoogleDriveAPIinAndroid?【发布时间】:2020-09-2305:23:20【问题描述】:我能够使用GoogleDrive提供的API成功地将我的图像添加到GoogleDrive。我现在可以将其... 查看详情
使用 API 从 Google 日历中检索事件
】使用API从Google日历中检索事件【英文标题】:RetrieveEventsfromGoogleCalendarUsingAPI【发布时间】:2014-04-2217:29:03【问题描述】:我是GoogleCalendarAPI和PHP的新手,我似乎不知道如何实现我想做的事情。基本上,我想从带有webapps098@gmail.c... 查看详情
如何从 Java 中的 MimeMessage 电子邮件中检索正文
】如何从Java中的MimeMessage电子邮件中检索正文【英文标题】:HowtoretrievethebodyfromaMimeMessageemailinJava【发布时间】:2015-03-2904:16:00【问题描述】:我正在尝试获取MIMEMessage的正文内容。电子邮件本质上是一个保存为文本文件的.EML文... 查看详情
java示例代码_从Java中的超类上下文中检索最低子类的名称
java示例代码_从Java中的超类上下文中检索最低子类的名称 查看详情
从 Java 中的 XML 字符串中检索属性值
】从Java中的XML字符串中检索属性值【英文标题】:RetrievingAttributeValuesfromaXMLstringinJava【发布时间】:2021-05-2517:42:15【问题描述】:我有以下XML字符串<Result><Component>PEVCR</Component><ComponentTextxml:lang="pt-BR"/><Componen... 查看详情
通过 XMPP 与 talk.google.com 卷曲
】通过XMPP与talk.google.com卷曲【英文标题】:curlwithtalk.google.comviaXMPP【发布时间】:2012-11-2619:00:18【问题描述】:我想使用libcurl用C++编写一个简单的googletalk客户端。Googletalk使用XMPP作为通信协议,它在talk.google.com:5222上侦听。首... 查看详情
java示例代码_使用java从mongo db中嵌入在数组中的内部文档中检索数据
java示例代码_使用java从mongo db中嵌入在数组中的内部文档中检索数据 查看详情