我最近读过关于XMPP的内容,我想创建一个可以发送和接收IM消息的应用程序,这样我就可以获得使用XMPP的一些经验。问题是我对使用XMPP或在Android上使用它几乎一无所知。我想知道是否有人可以指出我如何在Android上使用XMPP的正确方向。
谢谢!
答案 0 :(得分:1)
从XMPP开始,你必须
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.接下来你需要实现处理连接失败的原则。
public class XMPPConnectionFailedException extends 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 String nickname){ System.out.println(“连接聊天...”); class 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环境。