0

我最近阅读了有关 XMPP 的内容,我想制作一个可以发送和接收 IM 消息的应用程序,这样我就可以获得一些使用 XMPP 的经验。问题是我对使用 XMPP 或在 Android 上使用它几乎一无所知。我想知道是否有人可以为我指出如何在 Android 中使用 XMPP 的正确方向。

谢谢!

4

1 回答 1

1

好吧,从 XMPP 开始,你必须

  1. 安装OpenFire(聊天服务器)
  2. smack.jar XMPP 客户端 jar 添加到 Android 应用程序中
  3. 像这样实现 PacketListener
public class MyPacketListener implements PacketListener {

@Override     
public void processPacket(Packet packet) {        
   // Write the implementation code here.

//The packet contains the message and the metadata about the message.

}

}

4.接下来你需要实现优雅地处理连接失败。

公共类 XMPPConnectionFailedException 扩展 RuntimeException {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Override
public String toString() {

    return "The Chat server or the Connection to the chat server failed";
}

}

5.接下来,您需要实际连接到 XMPP 服务器的类,这是一个实现

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smackx.PrivateDataManager;

import com.test.chat.MyPacketListener;
import com.testchat.exception.XMPPConnectionFailedException;

public class ChatUtil {

public final static String SERVER_HOST = "serverip";
public final static int SERVER_PORT = 5222;
public final static String SERVICE_NAME = "p2547738.pubip.serverbeach.com";

private static XMPPConnection xmppConnection;

public static String CURRENT_RECIPIENT_CHAT_ID;
public static String CURRENT_RECIPIENT_NAME;

public static final String IN = "IN";
public static final String OUT = "OUT";
public static String CURRENT_RECIPIENT_FB_IMAGE;
public static boolean STARTED = false;

public static XMPPConnection getXmppConnection(String username) throws XMPPConnectionFailedException {

    try {
        if (xmppConnection == null) {
            ConnectionConfiguration config = new ConnectionConfiguration(SERVER_HOST, SERVER_PORT, SERVICE_NAME);
            xmppConnection = new XMPPConnection(config);

        }

        if (!xmppConnection.isConnected()) {
            xmppConnection.connect();
        }


        if (!xmppConnection.isAuthenticated()) {
            xmppConnection.login(username, "123");
            ProviderManager pm = ProviderManager.getInstance();
            pm.addIQProvider("query", "jabber:iq:private",new PrivateDataManager.PrivateDataIQProvider());

            PacketFilter packetFilter = new MessageTypeFilter(Message.Type.chat);

            xmppConnection.addConnectionListener(new ConnectionListener() {

                   @Override
                   public void reconnectionSuccessful() {
                       System.out.println("reconnectionSuccessful");
                   }

                   @Override
                   public void reconnectionFailed(Exception arg0) {
                       System.out.println("reconnectionFailed");
                   }

                   @Override
                   public void reconnectingIn(int arg0) {
                       System.out.println("reconnectingIn");
                   }

                   @Override
                   public void connectionClosedOnError(Exception arg0) {
                       System.out.println("connectionClosedOnError");
                   }

                   @Override
                   public void connectionClosed() {
                       System.out.println("connectionClosed");
                   }
               });

            MyPacketListener listener = new MyPacketListener();
            xmppConnection.addPacketListener(shareFareChatListener,packetFilter);


        }
        Presence presence = new Presence(Presence.Type.available);
        xmppConnection.sendPacket(presence);

        ChatUtil.STARTED = true;

    } catch (Exception e) {
        throw new XMPPConnectionFailedException();
    }

    return xmppConnection;

}

}

6.最后,您尝试使用您用来注册用户的凭据连接到 XMPP 服务器

private void connectToChat(final Stringnickname) { System.out.println("连接聊天..."); 类 ConnectToChatAsync 扩展 AsyncTask {

            @Override
            protected Integer doInBackground(Context... params) {

                try {
                    Listener.currentActivity = context;
                    ChatUtil.getXmppConnection(nickname);

                    return SERVER_SUCCESS_RESPONSE;
                } 

                catch ( XMPPConnectionFailedException e) {
                    System.err.println("XMPPConnectionFailedException : " + e);
                }

                return CONNECTIVITY_PROBLEM;
            }

            @Override
            protected void onPostExecute(Integer result) {
                super.onPostExecute(result);

                if(result == CONNECTIVITY_PROBLEM){
//                      ApplicationDialog.showFeedBackDialog(ProjectConstants.XMPP_CHAT_FAILED, context);
                }
            }
        }

        new ConnectToChatAsync().execute();
    }

那应该以编程方式解决您的问题,您所要做的就是设置 Openfire 环境。

这些链接应该可以帮助您了解该部门

  1. 创建您自己的与 iChat 或任何 Jabber 客户端一起使用的基于 Jabber 的服务器
  2. DIY:设置 Openfire 内部聊天服务器
于 2013-07-05T19:31:35.060 回答