我正在开发一个android应用程序,我一直在尝试连接到本地安装的openfire服务器。服务器已启动并运行,但当我尝试在手机上运行该应用程序时,它无法连接到服务器。
代码:
public class ChatService extends Service implements ConnectionListener {
private static Configuration configuration;
private static final String TAG = ChatService.class.getSimpleName();
public static void setupConnection(){
configuration = new Configuration();
final ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(configuration.getHost(), configuration.getPort(), configuration.getServiceName());
Log.d(TAG, "Host: " + configuration.getHost());
Log.d(TAG, "Port: " + configuration.getPort());
Log.d(TAG, "Service name: " + configuration.getServiceName());
Thread t = new Thread(new Runnable() {
@Override
public void run() {
configuration.setDebuggerEnabled(true);
XMPPConnection connection = new XMPPTCPConnection(connectionConfiguration);
try {
connection.connect();
if(connection.isConnected()){
Log.d(TAG, "Connected to server");
}
} catch (SmackException.ConnectionException e) {
for (int i = 0; i < e.getFailedAddresses().size(); i++) {
HostAddress element = e.getFailedAddresses().get(i);
Log.e("ERROR", element.getErrorMessage().toString());
}
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
}
});
t.start();
}
在logcat中,我得到了SmackException$ConnectException。
日志:
11-10 00:48:20.400 18449-18860/com.example.puneet.chat E/ERROR﹕ localhost:5222 Exception: failed to connect to localhost/127.0.0.1 (port 5222): connect failed: ECONNREFUSED (Connection refused)
11-10 00:48:20.408 18449-18860/com.example.puneet.chat W/System.err﹕ org.jivesoftware.smack.SmackException$ConnectionException
11-10 00:48:20.408 18449-18860/com.example.puneet.chat W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:433)
11-10 00:48:20.408 18449-18860/com.example.puneet.chat W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:808)
11-10 00:48:20.408 18449-18860/com.example.puneet.chat W/System.err﹕ at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:396)
11-10 00:48:20.408 18449-18860/com.example.puneet.chat W/System.err﹕ at XMPPConnection.ChatService$1.run(ChatService.java:45)
11-10 00:48:20.408 18449-18860/com.example.puneet.chat W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
如果有人能帮助我理解为什么我会得到这个异常,以及哪里出了问题,我将不胜感激。
发布于 2014-11-10 16:10:01
将configuration.host设置为
http://127.0.0.1:5222
然后试一试
如果仍然有问题,请转到Openfire >服务器>服务器设置>客户端连接,然后检查具有SSL (通常为5223)和的端口号,启用SSL并使用具有5223端口号的主机。
http://127.0.0.1:5223
发布于 2015-07-04 01:08:55
当您从localhost
连接仿真器时。您不使用127.0.0.1
ip,而是使用10.0.2.2
连接到localhost
服务器。
https://stackoverflow.com/questions/26837628
复制相似问题