XMPP的Facebook无法发送消息 [英] xmpp facebook not send message

查看:288
本文介绍了XMPP的Facebook无法发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个Android开源项目可供选择:里奇 - 毕姆项目以及连接Facebook的聊天,你可以使用下面的指南

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

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

  

=>对于你必须复制下面的现有项目三个文件

     

MemorizingTrustManager / src目录/德/ duenndns / SSL / MTMDecision.java   MemorizingTrustManager / src目录/德/ duenndns / SSL / MemorizingActivity.java   MemorizingTrustManager / src目录/德/ duenndns / SSL / MemorizingTrustManager.java   =>而在值添加以下值/ string.xml

 <资源>
    <字符串名称=mtm_accept_cert>接受未知的证书<?/串>
    <字符串名称=mtm_decision_always>总是LT; /串>
    <字符串名称=mtm_decision_once>在< /串>
    <字符串名称=mtm_decision_abort>中止< /串>
    <字符串名称=mtm_notification>证书验证< /串>
< /资源>
 

第二步,而不是使用SASLAuthentication如X-FACEBOOK平台,便可以使用下面的code与Facebook和登录使用您的Facebook的Jabber ID连接的,(username@chat.facebook.com)

 公共无效connectToFb()抛出XMPPException {

ConnectionConfiguration配置=新ConnectionConfiguration(chat.facebook.com,5222);
config.setSASLAuthenticationEnabled(真正的);
config.setSecurityMode(SecurityMode.required);
config.setRosterLoadedAtLogin(真正的);
config.setTruststorePath(/系统的/ etc /安全/ cacerts.bks);
config.setTruststorePassword(的changeit);
config.setTruststoreType(BKS);
config.setSend presence(假);
尝试 {
    的SSLContext SC = SSLContext.getInstance(TLS);
    sc.init(空,MemorizingTrustManager.getInstanceList(本),新java.security.SecureRandom中的());
    config.setCustomSSLContext(SC);
}赶上(GeneralSecurityException E){
    Log.w(TAG,无法使用MemorizingTrustManager,E);
}
XMPPConnection XMPP =新XMPPConnection(配置);
尝试 {
    xmpp.connect();
    xmpp.login(facebookusername,****); //在这里,你必须从facebookusername@chat.facebook.com只facebookusername
    名册名册= xmpp.getRoster();
    收藏< RosterEntry>条目= roster.getEntries();
    的System.out.println(保持通话!);
    的System.out.println(\ñ\ N+ entries.size()+好友(IES):);
    //显示第一次onliners ---->
    字符串临时[] =新的String [50];
    INT I = 0;
    对于(RosterEntry项:项){
        字符串用户= entry.getUser();
        Log.i(TAG,用户);
    }
}赶上(XMPPException E){
    xmpp.disconnect();
    e.printStackTrace();
}
}
 

上面的例子中运行,但不能发送message.How我可以发送邮件?

解决方案

希望这会使用你。

 弦乐味精= edtMsg.getText()的toString()修剪()。
    edtMsg.setText();

    如果(!msg.equals()){
        消息msgObj =新的消息(xxx@chat.facebook.com,Message.Type.chat);
        msgObj.setBody(MSG);
        connection.sendPacket(msgObj);
    }
 

There is one Android Open Source Project available : Beem Project and For connecting Facebook chat you can used following guidelines.

Steps for implementing Facebook chat API in Android:

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>

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

above example run but not send message.How can I send the message?

解决方案

Hope this will Use for You.

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

这篇关于XMPP的Facebook无法发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆