2016-02-26 100 views
-1

我有点新的Android我试图建立一个聊天安卓应用 使用openfire作为服务器和sma 4.1.6 libs 现在我试图做一个基本的连接我正在关注的咂嘴文档Android的XMPP基本连接

XMPPTCPConnectionConfiguration.Builder conf = XMPPTCPConnectionConfiguration.builder(); 
    conf.setUsernameAndPassword("user", "user"); 
    conf.setResource("android"); 
    conf.setServiceName("my_ip_adress"); 
    conf.setHost("my_ip_adress"); 
    conf.setPort(5222); 
    conf.setSecurityMode(ConnectionConfiguration.SecurityMode.ifpossible); 


    AbstractXMPPConnection connection=new XMPPTCPConnection(conf.build()); 
    try { 
     Log.e("CONNECTION====>", "try"); 
     connection.connect(); 
     Log.e("CONNECTION====>", "success"); 
     connection.login(); 
     Log.e("LOGIN====>", "success"); 
    } catch (SmackException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (XMPPException e) { 
     e.printStackTrace(); 
    } 

我在我的日志中获得此

E/CONNECTION====>: try 
02-26 17:59:14.995 15119-15145/com.example.firas.simplexmpp W/System.err: org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: '172.16.50.52:5222' failed because java.net.ConnectException: failed to connect to /172.16.50.52 (port 5222) after 30000ms: connect failed: ENETUNREACH (Network is unreachable) 

这些都是我的依赖

compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'org.igniterealtime.smack:smack-android:4.1.6' 
    compile 'org.igniterealtime.smack:smack-tcp:4.1.6' 
    compile 'org.igniterealtime.smack:smack-im:4.1.6' 
    compile 'org.igniterealtime.smack:smack-extensions:4.1.6' 
+0

如果您的客户端不在服务器的网络中,则必须使用服务器的真实IP。另外,如果你的服务器在NAT后面,你必须打开TCP端口(5222) – cherif

回答

1

检查您的手机或模拟器具有万一互联网连接

private boolean isNetworkAvailable() { 
ConnectivityManager connectivityManager 
    = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
return activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
} 
0

你好,这“会帮助任何人在未来我已经在ConnectionConfiguration.SecurityMode设置为禁用,并且工作正常

02-28 11:00:55.651 30656-30682/com.example.firas.simplexmpp E/CONNECTION====>: try 
02-28 11:00:55.721 30656-30682/com.example.firas.simplexmpp E/CONNECTION====>: success 
02-28 11:00:56.162 30656-30682/com.example.firas.simplexmpp E/LOGIN====>: success