簡體   English   中英

使用Android上的Smack API連接到localhost上的ejabberd XMPP服務器

[英]Connecting to a ejabberd XMPP server on localhost using Smack API on android

我在Mac上本地安裝了ejabberd XMPP服務器。 我正在使用此代碼通過android上的Smack API進行連接和登錄。

config = XMPPTCPConnectionConfiguration.builder()
                .setUsernameAndPassword("1@davids-macbook-pro.local", "1")
                .setHost("192.168.1.2")
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setServiceName("192.168.1.2")
                .setPort(Integer.parseInt("5222"))
                .build();

        AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
        try {
            conn2.connect();
            conn2.login();
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMPPException e) {
            e.printStackTrace();
        }

使用相同的用戶名和密碼,我可以使用任何其他XMPP客戶端(如Adium)登錄,但是上面的代碼在android上給出了此錯誤-

錯誤為org.jivesoftware.smack.XMPPException $ StreamErrorException的連接已關閉:主機未知

我的本地地址是192.168.1.2 ,ejabberd管理面板是localhost:5280 / admin

我閱讀了文檔,並做了所有書面工作。 代碼有問題嗎?

  • 用戶名應附加“ @”
  • setDebuggerEnabled(true)以查看幕后發生的事情
  • 將connect()和login()方法移至后台線程
  • 使用連接偵聽器成功建立連接后,調用login()

config = XMPPTCPConnectionConfiguration.builder()
                .setUsernameAndPassword("1@davids-macbook-pro.local", "1")
                .setHost("192.168.1.2")
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setServiceName("davids-macbook-pro.local")
                .setPort(5222)
                .setDebuggerEnabled(true) // to view what's happening in detail
                .build();

AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
try {
    conn2.connect(); // move it to a background thread instead of main thread
    // conn2.login();
} catch (SmackException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (XMPPException e) {
    e.printStackTrace();
}


conn2.addConnectionListener(new ConnectionListener() {
    @Override
    public void connected(XMPPConnection connection) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    conn2.login();
                } catch (SmackException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XMPPException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute();
    }

    @Override
    public void authenticated(XMPPConnection connection, boolean resumed) {

    }

    @Override
    public void connectionClosed() {

    }

    @Override
    public void connectionClosedOnError(Exception e) {

    }

    @Override
    public void reconnectionSuccessful() {

    }

    @Override
    public void reconnectingIn(int seconds) {

    }

    @Override
    public void reconnectionFailed(Exception e) {

    }
});

我以這種方式使它能夠連接並登錄到服務器-

config = XMPPTCPConnectionConfiguration.builder()
                .setUsernameAndPassword("1", "1")
                .setHost("192.168.1.2")
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setServiceName("davids-macbook-pro.local")
                .setPort(Integer.parseInt("5222"))
                .build();

        AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
        try {
            conn2.connect();
            conn2.login();
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMPPException e) {
            e.printStackTrace();
        }

您應該嘗試以下:

  1. 嘗試將端口更改為5280或5227
  2. 檢查服務器安全性配置是否為必需或可選,因為您已將安全性設置為禁用
  3. 將連接偵聽器添加到您的xmpp連接
  4. setDebugEnable(true)來檢查控制台中的smack日志並將日志添加到您的問題,這將使問題更加清楚
  5. 嘗試使用XMPPTCPConnectionConfiguration.SecurityMode.requiredXMPPTCPConnectionConfiguration.SecurityMode.optional更改ConnectionConfiguration.SecurityMode.disabled

此代碼應適用於smack 4.2.4

XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()
            .setHostAddress(InetAddress.getByName("192.168.1.2"))
            .setUsernameAndPassword("1@davids-macbook-pro.local", "1")
            .setXmppDomain(JidCreate.domainBareFrom("localhost"))
            .setResource("Rooster")
            .setKeystoreType(null) 
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .setCompressionEnabled(true).build();

將setHost()替換為setHostAddress()。 我不知道為什么,但是Windows服務器上的Ejabberd為我提供了TimeOut。 如果您在Windows上使用它,請同時使用基於Linux的OS進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM