android 用openfire做服务器,smack来连接怎么连接不上
展开全部
给你个例子
连接类
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
public class Conn {
private static XMPPConnection connection = null ;
public static XMPPConnection getConnection(String domain)
throws XMPPException {
XMPPConnection connection = new XMPPConnection(domain);
connection.connect();
return connection;
}
public static XMPPConnection getConnection(String domain, int port)
throws XMPPException {
if(connection==null){
ConnectionConfiguration config = new ConnectionConfiguration(domain,port);
connection = new XMPPConnection(config);
connection.connect();
}
return connection;
}
}
测试类
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
public class Test2 {
public static boolean LongAccount(XMPPConnection connection,
String username, String password) {
try {
connection.login(username, password);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) throws XMPPException {
XMPPConnection connection = Conn.getConnection("192.168.1.117");
boolean t = LongAccount(connection, "qeekey@localhost.localdomain", "password");
System.out.println("t login =" + t);
if(t){
ChatManager chatmanager = connection.getChatManager();
//新建一个会话
Chat newChat = chatmanager.createChat("lxm@localhost.localdomain", new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("start me , Received from 【" + message.getFrom() + "】 message: " + message.getBody());
}
});
//发送消息
newChat.sendMessage("我是菜鸟");
}
}
转载,仅供参考。
连接类
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
public class Conn {
private static XMPPConnection connection = null ;
public static XMPPConnection getConnection(String domain)
throws XMPPException {
XMPPConnection connection = new XMPPConnection(domain);
connection.connect();
return connection;
}
public static XMPPConnection getConnection(String domain, int port)
throws XMPPException {
if(connection==null){
ConnectionConfiguration config = new ConnectionConfiguration(domain,port);
connection = new XMPPConnection(config);
connection.connect();
}
return connection;
}
}
测试类
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
public class Test2 {
public static boolean LongAccount(XMPPConnection connection,
String username, String password) {
try {
connection.login(username, password);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) throws XMPPException {
XMPPConnection connection = Conn.getConnection("192.168.1.117");
boolean t = LongAccount(connection, "qeekey@localhost.localdomain", "password");
System.out.println("t login =" + t);
if(t){
ChatManager chatmanager = connection.getChatManager();
//新建一个会话
Chat newChat = chatmanager.createChat("lxm@localhost.localdomain", new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("start me , Received from 【" + message.getFrom() + "】 message: " + message.getBody());
}
});
//发送消息
newChat.sendMessage("我是菜鸟");
}
}
转载,仅供参考。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |