[英]Connecting with server using smack API in MainActivity android
[英]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)
以查看幕后發生的事情 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();
}
您應該嘗試以下:
XMPPTCPConnectionConfiguration.SecurityMode.required
或XMPPTCPConnectionConfiguration.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.