gpt4 book ai didi

android - xmpp facebook 不发送消息

转载 作者:行者123 更新时间:2023-11-29 18:09:42 25 4
gpt4 key购买 nike

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

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

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

=> 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>

第二步,您可以使用以下代码连接 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();
}
}

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

最佳答案

希望这对你有用。

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

关于android - xmpp facebook 不发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11395185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com