2012-07-09 534 views
1

有一個Android開源項目可用:Beem項目和連接Facebook聊天,您可以使用以下指南。xmpp facebook未發送消息

步驟在Android的實實現聊天API:

首先,我們必須實現在現有項目MemorizingTrustManager庫項目。

=>對於您需要複製現有項目的以下三個文件

MemorizingTrustManager/src目錄/德/ duenndns/SSL/MTMDecision.java MemorizingTrustManager/src目錄/德/ duenndns/SSL/MemorizingActivity。的java MemorizingTrustManager/SRC/DE/duenndns/SSL/MemorizingTrustManager.java =>而在值/ string.xml添加下列值

<resources> 
    <string name="mtm_accept_cert">Accept Unknown Certificate?</string> 
    <string name="mtm_decision_always">Always</string> 
    <string name="mtm_decision_once">Once</string> 
    <string name="mtm_decision_abort">Abort</string> 
    <string name="mtm_notification">Certificate Verification</string> 
</resources> 

秒s TEP,代替使用SASLAuthentication諸如X FACEBOOK平臺的,可以使用下面的代碼來與Facebook和登錄使用您的Facebook的Jabber ID([email protected])連接

public void connectToFb() throws XMPPException { 

ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222); 
config.setSASLAuthenticationEnabled(true); 
config.setSecurityMode(SecurityMode.required); 
config.setRosterLoadedAtLogin(true); 
config.setTruststorePath("/system/etc/security/cacerts.bks"); 
config.setTruststorePassword("changeit"); 
config.setTruststoreType("bks"); 
config.setSendPresence(false); 
try { 
    SSLContext sc = SSLContext.getInstance("TLS"); 
    sc.init(null, MemorizingTrustManager.getInstanceList(this), new java.security.SecureRandom()); 
    config.setCustomSSLContext(sc); 
} catch (GeneralSecurityException e) { 
    Log.w("TAG", "Unable to use MemorizingTrustManager", e); 
} 
XMPPConnection xmpp = new XMPPConnection(config); 
try { 
    xmpp.connect(); 
    xmpp.login("facebookusername", "****"); // Here you have to used only facebookusername from [email protected] 
    Roster roster = xmpp.getRoster(); 
    Collection<RosterEntry> entries = roster.getEntries(); 
    System.out.println("Connected!"); 
    System.out.println("\n\n" + entries.size() + " buddy(ies):"); 
    // shows first time onliners----> 
    String temp[] = new String[50]; 
    int i = 0; 
    for (RosterEntry entry : entries) { 
     String user = entry.getUser(); 
     Log.i("TAG", user); 
    } 
} catch (XMPPException e) { 
    xmpp.disconnect(); 
    e.printStackTrace(); 
} 
} 

上面的例子中運行但不發送消息。我可以如何發送消息?

回答

1

您必須在成功登錄後設置連接。

/** * 通過設置對話框時調用連接establised與XMPP *服務器 * *參數連接 */

public void setConnection(XMPPConnection connection) { 

     this.connection = connection; 
     if (connection != null) { 
      // Add a packet listener to get messages sent to us 
      PacketFilter filter = new MessageTypeFilter(Message.Type.chat); 
      connection.addPacketListener(new PacketListener() { 
       public void processPacket(Packet packet) { 
        Message message = (Message) packet; 
        if (message.getBody() != null) { 
         String fromName = StringUtils.parseBareAddress(message 
           .getFrom()); 
         Log.i("XMPPClient", "Got text [" + message.getBody() 
           + "] from [" + fromName + "]"); 
         messages.add(fromName + ":"); 
         messages.add(message.getBody()); 
         // Add the incoming message to the list view 
         mHandler.post(new Runnable() { 
          public void run() { 
           setListAdapter(); 
          } 
         }); 
        } 
       } 
      }, filter); 
     } 
    } 



private void setListAdapter() { 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       R.layout.multi_line_list_item, messages); 
     mList.setAdapter(adapter); 
    } 
+0

連接成功。收到消息列表成功但不發送消息。如何發送消息?例如:消息msg =新消息(「xxxxx」,Message.Type.chat); msg.setBody(「request」); connection.sendPacket(msg); setConnection(connection);不起作用 – Batuhan 2012-07-10 20:03:05

+0

選中此鏈接,可能對我們有用:http://davanum.wordpress.com/2008/12/29/updated-xmpp-client-for-android/ http://samse.tistory。 com/entry/Android-Smack-example – 2012-07-11 04:16:56

+0

我看了,但無法發送消息:( – Batuhan 2012-07-11 11:17:50

2

希望這會爲你使用。

 String msg = edtMsg.getText().toString().trim(); 
    edtMsg.setText(""); 

    if(!msg.equals("")){ 
     Message msgObj = new Message("[email protected]", Message.Type.chat); 
     msgObj.setBody(msg); 
     connection.sendPacket(msgObj); 
    } 
+0

將所有消息存儲在ArrayList中,並且每次都在UI上設置適配器, – 2012-07-21 07:16:47

+0

如何添加帶有消息標記的戳記屬性?因爲我需要發送郵戳時發送消息和顯示發送時間當它顯示在設備上 – 2012-09-17 13:14:51

+0

@GautamVasoya我面臨問題在發送消息通過xmpp,你可以過來這[鏈接](http://chat.stackoverflow.com/rooms/68853/trying-學習)來幫助解決問題 – 2015-01-19 10:36:58