我正在使用asmack api为Gtalk创建一个聊天应用程序,我希望在用户离线时获取消息,但由于ServiceDiscoveryManager中的一些问题及其说该功能未实现(501)而试图解决问题并试图实现其他用户遇到同样问题的所有事情,但现在我得到了这个错误。我发布代码和logcat与此。任何帮助表示感谢。
ConnectionConfiguration connConfig = new ConnectionConfiguration(
host, Integer.parseInt(port), service);
connConfig.setSASLAuthenticationEnabled(true);
connConfig.setSendPresence(false);
connection = new XMPPConnection(connConfig);
connection.connect();
connection.login(username, password);
ServiceDiscoveryManager sdm= ServiceDiscoveryManager.getInstanceFor(connection);
mOfflineMessageManager = new OfflineMessageManager(connection);
offlinemsgs = mOfflineMessageManager.getMessageCount();
这是我在登录后立即调用离线消息的代码,下面是logcat错误中的响应:
03-16 11:26:53.871: W/System.err(325): feature-not-implemented(501)
03-16 11:26:53.881: W/System.err(325): at org.jivesoftware.smackx.OfflineMessageManager.getMessages(OfflineMessageManager.java:210)
03-16 11:26:53.881: W/System.err(325): at com.apache.android.xmpp.MainScreen.getOfflinemessages(MainScreen.java:911)
03-16 11:26:53.881: W/System.err(325): at com.apache.android.xmpp.MainScreen$LogIn.doInBackground(MainScreen.java:612)
03-16 11:26:53.881: W/System.err(325): at com.apache.android.xmpp.MainScreen$LogIn.doInBackground(MainScreen.java:1)
03-16 11:26:53.881: W/System.err(325): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-16 11:26:53.881: W/System.err(325): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-16 11:26:53.881: W/System.err(325): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-16 11:26:53.881: W/System.err(325): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-16 11:26:53.881: W/System.err(325): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-16 11:26:53.891: W/System.err(325): at java.lang.Thread.run(Thread.java:1096)
请帮助解决这个问题,尝试从很多天开始解决问题,但无法找到解决方案。
答案 0 :(得分:7)
试试这个:
ConnectionConfiguration connConfig = new ConnectionConfiguration(
host, Integer.parseInt(port), service);
connConfig.setSASLAuthenticationEnabled(true);
connConfig.setSendPresence(false);
connection = new XMPPConnection(connConfig);
connection.connect();
connection.login(username, password);
ServiceDiscoveryManager sdm= ServiceDiscoveryManager.getInstanceFor(connection);
////////////////////////////
OfflineMessageManager offlineManager = new OfflineMessageManager(
Client.getConnection());
try {
Iterator<org.jivesoftware.smack.packet.Message> it = offlineManager
.getMessages();
System.out.println(offlineManager.supportsFlexibleRetrieval());
System.out.println("Number of offline messages:: " + offlineManager.getMessageCount());
Map<String,ArrayList<Message>> offlineMsgs = new HashMap<String,ArrayList<Message>>();
while (it.hasNext()) {
org.jivesoftware.smack.packet.Message message = it.next();
System.out
.println("receive offline messages, the Received from [" + message.getFrom()
+ "] the message:" + message.getBody());
String fromUser = message.getFrom().split("/")[0];
if(offlineMsgs.containsKey(fromUser))
{
offlineMsgs.get(fromUser).add(message);
}else{
ArrayList<Message> temp = new ArrayList<Message>();
temp.add(message);
offlineMsgs.put(fromUser, temp);
}
}
/ / Deal with a collection of offline messages ...
Set<String> keys = offlineMsgs.keySet();
Iterator<String> offIt = keys.iterator();
while(offIt.hasNext())
{
String key = offIt.next();
ArrayList<Message> ms = offlineMsgs.get(key);
TelFrame tel = new TelFrame(key);
ChatFrameThread cft = new ChatFrameThread(key, null);
cft.setTel(tel);
cft.start();
for (int i = 0; i < ms.size(); i++) {
tel.messageReceiveHandler(ms.get(i));
}
}
offlineManager.deleteMessages();
} catch (Exception e) {
e.printStackTrace();
}
答案 1 :(得分:0)
为什么你不只是使用PacketListener我正在使用它并获取离线消息。这是我的代码我也将消息保存到Sqlite数据库以获取用户消息一段时间后他离开这个聊天并与另一个朋友聊天然后去回来。
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
Main.conn.addPacketListener(new PacketListener() {
public void processPacket(Packet p) {
message = (Message) p;// this was the problem
if (message.getBody() != null) {
messages.add(message.getBody());
Log.i("XMPPClient",
"Reciveing text [" + message.getBody() + "] ");
time.add(TimeDate());
// Add the incoming message to the list view
mHandler.post(new Runnable() {
public void run() {
// / saving convertsations into SQLite db
try {
save.open();
save.InsertIntoDatabase(jid, messages, time);
messagesDB = save.GetAllValues(Table_Name,
new String[] { "messages" },
friendJid);
timeDB = save.GetAllValues(Table_Name,
new String[] { "time" }, friendJid);
adapter = new ConversationListAdapter(
getApplicationContext(),
messagesDB, timeDB);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
save.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
}, filter);
}
答案 2 :(得分:0)
我在使用连接到Openfire服务器的aSmack开发Android客户端时遇到了同样的问题:我根本无法检索离线消息。所以阅读[XEP-0013]:http://www.xmpp.org/extensions/xep-0013.html我想出了原因:
“收到发往”http://jabber.org/protocol/offline“节点的服务发现请求(如本用例中的disco #info请求或下一个用例中的disco#items请求),服务器如果用户随后在此会话期间向服务器发送初始状态,则不得发送大量离线消息。因此,用户现在可以自由发送初始状态(如果需要)并参与正常IM活动,同时继续阅读脱机消息。“
所以,我学到了什么:
为了使工作正常,您必须连接,将初始状态发送为“不可用”以便检索您的消息,然后在此之后将状态发送为“可用”。
然后你的代码看起来像这样(try / catch block ommited):
SmackAndroid.init(context);
AndroidConnectionConfiguration connConfig = null;
connConfig = new AndroidConnectionConfiguration(domain, PORT);
connConfig.setSendPresence(true);
connConfig.setReconnectionAllowed(true);
connection = new XMPPConnection(connConfig);
connection.connect();
connection.login("username", "password");
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
//RECEIVER
connection.addPacketListener(getPacketListener(), filter);
//in order to retrieve offline messages: XEP-0013
connection.sendPacket(new Presence(Presence.Type.unavailable));
connection.sendPacket(new Presence(Presence.Type.available));
然后在发送Presence.Type.unavailable数据包后,所有离线消息都是自动发送的!