安卓Facebook聊天示例项目

6
我在这里找到了一个样例iOS的Facebook聊天项目(链接)

同时我也在这里得到了Facebook聊天文档(链接)

同样的,有人知道在Android实现Facebook聊天的示例应用程序或方式吗?

1个回答

9

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

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

  1. First we have to implement MemorizingTrustManager Library project in existing project.

    => For that you have to copy following three files in existing project

    • MemorizingTrustManager/src/de/duenndns/ssl/MTMDecision.java
    • MemorizingTrustManager/src/de/duenndns/ssl/MemorizingActivity.java
    • MemorizingTrustManager/src/de/duenndns/ssl/MemorizingTrustManager.java

    => And add following values in 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>
    
  2. Second step, Instead of using SASLAuthentication such as X-FACEBOOK-PLATFORM, You can used following code to connect with Facebook and login using your Facebook Jabber ID (username@chat.facebook.com)

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

最后,如果你在LogCat视图中获取了你的Facebook帐户的所有好友列表,则可以使用此教程实现简单的Facebook聊天。


1
我已经接受了答案,但如果有其他答案,欢迎新的回答;-) - Ganapathy C
我尝试了这种方法,但它只返回一组看起来与我的朋友有某种关联的随机数字列表。你有什么想法吗? - Peter
@Dipali 我得到了getAppKeyStore(/data/data/com.example.facebookchatdemo/app_KeyStore/KeyStore.bks) - 文件不存在,如何解决这个问题。请帮忙点赞 :) - Qadir Hussain
非常感谢.... :) 请问如何检查这里朋友的存在? - Nizam
为此,您必须将以下三个文件复制到现有项目中。请问要复制到哪里? - chaon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接