2012-03-16 61 views
4

我正在使用asmack api爲Gtalk創建聊天應用程序,並且我想在用戶離線時獲取消息,但是由於ServiceDiscoveryManager中的某些問題而產生問題該功能沒有實現(501),並試圖實現其他用戶都有同樣問題的所有事情,但現在我得到這個錯誤。我用這個發佈代碼和logcat。任何幫助讚賞。無法使用asmack api爲Gtalk應用程序獲取離線消息

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

嘿,你有解決方案嗎?我得到同樣的例外。 – 2012-04-21 06:18:02

+0

no。:(仍在尋找解決方法,告訴我,如果你有任何問題!) – 2012-04-21 08:27:03

+0

http://stackoverflow.com/questions/7454995/getting-offline-messages-in-android-chat-appplication-with-use- asmack-library 我收到了這個 – 2012-04-21 08:32:23

回答

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(); 
      } 

看到這個像:http://community.igniterealtime.org

+0

即時通訊使用相同的東西爲此,不工作:( – 2012-03-22 09:51:48

+0

,因爲沒有任何其他答案,你的答案似乎相當正確,我只是在谷歌服務的問題,但不是來自asmack api。感謝你的回答。 – 2012-03-27 10:41:58

+0

@imran khan什麼是最好的API XMPP客戶端必須支持Facebook和GTalk聊天的。如果我用asmack-issue5面臨gtalk問題,並面臨gtalk問題(獲得好友列表)。 – 2012-07-27 10:42:12

0

爲什麼ü不只是使用PacketListener我使用它,並得到離線消息。這裏是我的代碼我也保存郵件到SQLite的db在他離開這個聊天並與另一個朋友聊天然後回去之後獲得用戶消息。

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); 
     } 
+0

感謝這:)。但是我正在尋找離線消息,當我啓動應用程序時,我沒有收到消息,應該有些東西可以從中檢索脫機消息,我已經嘗試從此方法中獲取它們,但沒有任何工作。如果你知道如何獲得離線消息,請指導我。 – 2012-05-03 05:24:57

+0

@ mak_just4anything你好,你找到soln :(我仍然在尋找:( – 2012-11-04 08:45:20

0

我正在面對同樣的問題開發Android客戶端與連接到Openfire服務器的smack:我根本無法檢索離線消息。所以讀完[XEP-0013]:http://www.xmpp.org/extensions/xep-0013.html我想通了,爲什麼:

"Upon receiving a service discovery request addressed to a node of " http://jabber.org/protocol/offline " (either a disco#info request as in this use case or a disco#items request as in the next use case), the server MUST NOT send a flood of offline messages if the user subsequently sends initial presence to the server during this session. Thus the user is now free to send initial presence (if desired) and to engage in normal IM activities while continuing to read through offline messages."

所以,我學:

  1. 檢查,如果你的服務器支持靈活的離線消息檢索XEP-0013;
  2. 您必須初始化SmackAndroid.init(上下文) - 請參閱aSmack自述文件;
  3. 您必須使用AndroidConnectionConfiguration而不是ConnectionConfiguration;
  4. 如果需要,您可以將您的配置設置爲setSendPresence(true)。

爲了獲得正常工作,您必須連接,發送初始狀態爲「不可取消」,以便檢索您的消息,然後發送狀態爲「可用」。

那麼你的代碼應該看起來像這樣(試/中省略catch塊):

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包後,所有脫機消息被髮送automaticaly!

相關問題