Java连接open fire_连接到本地openfire服务器android

试试这个连接:

public static boolean XMPPConnect() {

try {

System.setProperty("java.net.preferIPv6Addresses", "false");

//SmackConfiguration.setPacketReplyTimeout(30000);

config = new ConnectionConfiguration(Constant._hostName, port);

//config = new AndroidConnectionConfiguration(Constant._hostName);

// config.setCompressionEnabled(true);

// config.setSASLAuthenticationEnabled(true);

// SmackConfiguration.setPacketReplyTimeout(1000*60);

// config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);

// config.setReconnectionAllowed(true);

// //config.setCompressionEnabled(true);

config.setRosterLoadedAtLogin(true);

config.setSendPresence(true);

//SASLAuthentication.supportSASLMechanism("MD5");

config.setSASLAuthenticationEnabled(true);

config.setCompressionEnabled(true);

config.setSecurityMode(SecurityMode.enabled);

config.setReconnectionAllowed(true);

SmackConfiguration.setPacketReplyTimeout(30000);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {

config.setTruststoreType("AndroidCAStore");

config.setTruststorePassword(null);

config.setTruststorePath(null);

}/*else if (Build.VERSION.SDK_INT<19 || Build.VERSION.SDK_INT>= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {

}*/ else {

config.setTruststoreType("BKS");

String path = System.getProperty("javax.net.ssl.trustStore");

if (path == null)

path = System.getProperty("java.home") + File.separator + "etc"

+ File.separator + "security" + File.separator

+ "cacerts.bks";

config.setTruststorePath(path);

}

connection = new XMPPConnection(config);

config.setSASLAuthenticationEnabled(true);

connection.connect();

debugEnabledReset();

} catch (Exception e) {

XMPPConstants.XMPP_ERROR="socket_timeout";

e.printStackTrace();

if(connection.DEBUG_ENABLED==true)

connection.DEBUG_ENABLED = false;

return false;

}

return true;

}

如果它返回true则登陆xmpp如下:

public static boolean XMPPLogin(String uname, String password) {

Roster roster = connection.getRoster();

roster.addRosterListener(new RosterListener() {

public void presenceChanged(Presence arg0) {}

public void entriesUpdated(Collection arg0) {}

public void entriesDeleted(Collection arg0) {}

public void entriesAdded(Collection arg0) {}

});

try {

//SASLAuthentication.supportSASLMechanism("MD5", 0);

connection.login(uname, password, "Smack");

} catch (Exception e) {

XMPPConstants.XMPP_ERROR="Username or password is incorrect";

if(e.getMessage().toString().contains("No response")){

XMPPConstants.XMPP_ERROR="Server communication failed";

}

e.printStackTrace();

if(connection.DEBUG_ENABLED==true)

connection.DEBUG_ENABLED = false;

return false;

}

return true;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值