无法连接到本地openfire XMPP服务器

时间:2014-11-10 05:55:13

标签: android xmpp asmack

我正在开发一个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)

如果有人能帮我理解为什么我会得到这个例外以及出了什么问题,我真的很感激

2 个答案:

答案 0 :(得分:1)

将configuration.host设置为

http://127.0.0.1:5222

然后尝试

如果仍有问题,请转到 Openfire&gt;服务器&gt;服务器设置&gt;客户端连接,然后检查具有 SSL(通常为5223)启用SSL 的端口号,并使用带有 5223端口号码的主机

http://127.0.0.1:5223

答案 1 :(得分:1)

localhost连接模拟器时。您不使用127.0.0.1 ip而是使用10.0.2.2连接到localhost服务器。