整合Facebook聊天与Asmack API的Android(Integrate Facebook

2019-07-30 02:31发布

我尝试实现聊天支持在建议这个博客 。

目前,我使用下面的代码来实现聊天界面。

Java代码

TestChatActivity.java

import java.util.ArrayList;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class TestChatActivity extends Activity implements OnClickListener {

private ArrayList<String> messages = new ArrayList();
private Handler mHandler = new Handler();
private EditText mRecipient;
private EditText mSendText;
private ListView mList;
private XMPPConnection mConnection;
private String mHost, mPort, mService, mUsername, mPassword;
private ConnectionConfiguration mConnConfig;
private String TAG;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    initLayout();
    // initGtalk();
    initFB();

    // Create a connection
    createConnection();

    // login
    loginToXMPP();

}

void initLayout() {
    Log.i("XMPPClient", "onCreate called");
    setContentView(R.layout.main);

    mRecipient = (EditText) this.findViewById(R.id.recipient);
    Log.i("XMPPClient", "mRecipient = " + mRecipient);
    mSendText = (EditText) this.findViewById(R.id.sendText);
    Log.i("XMPPClient", "mSendText = " + mSendText);
    mList = (ListView) this.findViewById(R.id.listMessages);
    Log.i("XMPPClient", "mList = " + mList);
    // Set a listener to send a chat text message
    Button send = (Button) this.findViewById(R.id.send);
    send.setOnClickListener(this);

    setListAdapter();
}

void initGtalk() {
    mHost = "talk.google.com";
    mPort = "5222";
    mService = "gmail";
    mUsername = "abc@gmail.com";
    mPassword = "password";
    // Set Default recipients for Gtalk
    mRecipient.setText("default_buddy@gmail.com");
}

void initFB() {
    mHost = "chat.facebook.com";
    mPort = "5222";
    mService = "xmpp";
    mUsername = "userid@chat.facebook.com";
    mPassword = "password";
    // Set Default recipients for FB
    mRecipient.setText("new_userid@chat.facebook.com");
}

void createConnection() {
    mConnConfig = new ConnectionConfiguration(mHost,
            Integer.parseInt(mPort), mService);
    mConnConfig.setSecurityMode(SecurityMode.required);
    mConnConfig.setSASLAuthenticationEnabled(true);
    mConnection = new XMPPConnection(mConnConfig);

    try {
        mConnection.connect();
        Log.i("XMPPClient",
                "[SettingsDialog] Connected to " + mConnection.getHost());
    } catch (XMPPException ex) {
        Log.e("XMPPClient", "[SettingsDialog] Failed to connect to "
                + mConnection.getHost());
        Log.e("XMPPClient", ex.toString());
        setConnection(null);
    }
}

void loginToXMPP() {
    try {
        mConnection.login(mUsername, mPassword);
        Log.i("XMPPClient", "Logged in as " + mConnection.getUser());

        // Set the status to available
        Presence presence = new Presence(Presence.Type.available);
        mConnection.sendPacket(presence);
        setConnection(mConnection);
    } catch (XMPPException ex) {
        Log.e("XMPPClient", "[SettingsDialog] Failed to log in as "
                + mUsername);
        Log.e("XMPPClient", ex.toString());
        setConnection(null);
    }
}

/**
 * Called by Settings dialog when a connection is established with the XMPP
 * server
 * 
 * @param connection
 */
public void setConnection(XMPPConnection connection) {
    this.mConnection = 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);
}

public void onClick(View v) {
    if (v.getId() == R.id.send) {
        String to = mRecipient.getText().toString();
        String text = mSendText.getText().toString();

        Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");
        Message msg = new Message(to, Message.Type.chat);
        msg.setBody(text);
        mConnection.sendPacket(msg);
        messages.add(mConnection.getUser() + ":");
        messages.add(text);
        setListAdapter();
    }

}
}

布局

main.xml中

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="70dp"
        android:text="Recipient:" />

    <EditText
        android:id="@+id/recipient"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoText="false"
        android:capitalize="none"
        android:minWidth="250dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textSize="16sp" />
</LinearLayout>

<ListView
    android:id="@+id/listMessages"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:scrollbars="horizontal" />

<EditText
    android:id="@+id/sendText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:autoText="false"
    android:capitalize="none"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:textSize="16sp" />

<Button
    android:id="@+id/send"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Send" >
</Button>

</LinearLayout>

multi_line_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:textStyle="bold" />

上面的代码使用Asmack

谷歌谈工作对我罚款但Facebook聊天不使用此代码工作。 :(

是否有用于Facebook的聊天连接的任何特殊配置? 或者这种方法错与Facebook聊天连接?

因为我已经做了很多R&d上和Facebook支持XMPP聊天支持在讨论Facebook的XMPP聊天设置等等,正如我在想,这应该是工作。

任何人都已经实现Facebook的聊天在机器人可以帮我解决这个问题。

提前致谢...!!!

Answer 1:

我已经检查的所有代码姆项目 ,并发现与Facebook聊天API连接解决方案。

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

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

    =>对于您需要复制以下在现有项目中三个文件

    • MemorizingTrustManager / src目录/德/ duenndns / SSL / MTMDecision.java
    • MemorizingTrustManager / src目录/德/ duenndns / SSL / MemorizingActivity.java
    • MemorizingTrustManager / src目录/德/ 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> 
  2. 第二步,而不是使用SASLAuthenticationX-Facebook的平台 ,您可以使用下面的代码与Facebook和登录使用您的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(); } } 

最后,如果你让你的Facebook帐户的所有好友列表中查看logcat中,除了可以使用实现简单的Facebook聊天本教程 。

更新 :我也在努力的工作像Facebook Messenger和即将在GitHub上发布的示例应用程序。



文章来源: Integrate Facebook chat in android with Asmack API