android 用openfire做服务器,smack来连接怎么连接不上

 我来答
匿名用户
推荐于2016-07-13
展开全部
给你个例子
连接类
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("我是菜鸟");
}
}
转载,仅供参考。
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式