xmpp facebook不发送消息

时间:2012-07-09 12:38:51

标签: android facebook xmpp

有一个Android开源项目可用:Beem Project和用于连接Facebook聊天,您可以使用以下指南。

在Android中实施Facebook聊天API的步骤:

首先,我们必须在现有项目中实现MemorizingTrustManager Library项目。

  

=>为此,您必须复制现有项目中的以下三个文件

     

MemorizingTrustManager / SRC / DE / duenndns / SSL / MTMDecision.java   MemorizingTrustManager / src目录/德/ duenndns / SSL / MemorizingActivity.java   MemorizingTrustManager / src目录/德/ duenndns / SSL / MemorizingTrustManager.java   =>并在values / 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>

第二步,您可以使用以下代码连接Facebook并使用您的Facebook Jabber ID(username@chat.facebook.com)登录,而不是使用X-FACEBOOK-PLATFORM等SASLAuthentication

    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 facebookusername@chat.facebook.com
    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();
}
}

以上示例运行但不发送消息。如何发送消息?

2 个答案:

答案 0 :(得分:2)

希望这会用于你。

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

    if(!msg.equals("")){
        Message msgObj = new Message("xxx@chat.facebook.com", Message.Type.chat);
        msgObj.setBody(msg);
        connection.sendPacket(msgObj);
    }

答案 1 :(得分:1)

您必须在登录成功后设置连接。

/ **      *与XMPP建立连接时,由“设置”对话框调用      *服务器      *      * @param连接      * /

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