有一個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();
}
}
上面的例子中運行但不發送消息。我可以如何發送消息?
連接成功。收到消息列表成功但不發送消息。如何發送消息?例如:消息msg =新消息(「xxxxx」,Message.Type.chat); msg.setBody(「request」); connection.sendPacket(msg); setConnection(connection);不起作用 – Batuhan 2012-07-10 20:03:05
選中此鏈接,可能對我們有用: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
我看了,但無法發送消息:( – Batuhan 2012-07-11 11:17:50