我正在尝试制作一个简单的聊天应用程序。我一直在研究这个,发现了图书馆。为此,我将所有库下载并在我的计算机中安装了openfire服务器,并在其中创建了少量用户进行测试。我终于可以连接到服务器,但后来我遇到了登录失败。我尝试使用来自spark的相同用户名登录,它运行正常。我在SASL身份验证上遇到错误。需要一些帮助。
这些是我的进口商品:
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.filter.StanzaTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jxmpp.jid.Jid;
这些是我的参数:
public static final String HOST = "192.168.1.114";
public static final int PORT = 5222;
public static final String SERVICE = "192.168.1.114"; // i dont know if its right..
public static final String USERNAME = "test";
public static final String PASSWORD = "test";
这是代码:
public void connect()
{
final ProgressDialog dialog = ProgressDialog.show(this, "Connecting...", "Please wait...", false);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// Create a connection
// ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT, SERVICE);
Log.v("checking", "011");
XMPPTCPConnectionConfiguration.Builder connConfig = XMPPTCPConnectionConfiguration.builder();
connConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); // enabled gives me SSL certificate authorization errors...
connConfig.setUsernameAndPassword(USERNAME, PASSWORD);
try {
connConfig.setServiceName(JidCreate.domainBareFrom(SERVICE));
} catch (XmppStringprepException e) {
e.printStackTrace();
Log.v("checking", e.toString() + "1");
}
connConfig.setHost(HOST);
connConfig.setPort(PORT);
connConfig.setDebuggerEnabled(true);
AbstractXMPPConnection connection = new XMPPTCPConnection(connConfig.build());
try {
connection.connect();
Log.v("XMPPChatDemoActivity", "[SettingsDialog] Connected to "+connection.getHost());
} catch (XMPPException ex) {
Log.v("XMPPChatDemoActivity", "[SettingsDialog] Failed to connect to "+ connection.getHost());
Log.v("XMPPChatDemoActivity", ex.toString());
setConnection(null);
} catch (SmackException e) {
e.printStackTrace();
Log.v("XMPPChatDemoActivity", e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.v("XMPPChatDemoActivity", e.toString());
} catch (InterruptedException e) {
e.printStackTrace();
Log.v("XMPPChatDemoActivity", e.toString());
}
try {
connection.login(USERNAME, PASSWORD);
Log.v("XMPPChatDemoActivity", "Logged in as" + connection.getUser());
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendStanza(presence);
setConnection((XMPPTCPConnection) connection);
Roster roster = Roster.getInstanceFor(connection);
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
Log.v("XMPPChatDemoActivity", "--------------------------------------");
Log.v("XMPPChatDemoActivity", "RosterEntry " + entry);
Log.v("XMPPChatDemoActivity", "User: " + entry.getUser());
Log.v("XMPPChatDemoActivity", "Name: " + entry.getName());
Log.v("XMPPChatDemoActivity", "Status: " + entry.getStatus());
Log.v("XMPPChatDemoActivity", "Type: " + entry.getType());
Presence entryPresence = roster.getPresence(entry.getUser());
Log.v("XMPPChatDemoActivity", "Presence Status: "+ entryPresence.getStatus());
Log.v("XMPPChatDemoActivity", "Presence Type: " + entryPresence.getType());
Presence.Type type = entryPresence.getType();
if (type == Presence.Type.available)
Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
Log.v("XMPPChatDemoActivity", "Presence : " + entryPresence);
}
} catch (XMPPException ex) {
Log.v("XMPPChatDemoActivity", "Failed to log in as "+ USERNAME + " or " + connection.getHost());
Log.v("XMPPChatDemoActivity", ex.toString());
setConnection(null);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
dialog.dismiss();
}
});
t.start();
dialog.show();
}
这是logcat的一个片段:
08-03 06:56:26.812 8824-8824/com.example.sumit.testapp V/ActivityThread﹕ updateVisibility : ActivityRecord{15e86073 token=android.os.BinderProxy@26aa149d {com.example.sumit.testapp/com.example.sumit.testapp.MainActivity}} show : true
08-03 06:56:26.902 8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <stream:stream xmlns='jabber:client' to='192.168.1.114' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='test@192.168.1.114' xml:lang='en'>
08-03 06:56:26.922 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="bahadurxxx" id="e61566be" xml:lang="en" version="1.0">
08-03 06:56:26.922 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</mechanism></mechanisms><compression xmlns="http://jabber.org/features/compress"><method>zlib</method></compression><auth xmlns="http://jabber.org/features/iq-auth"/><register xmlns="http://jabber.org/features/iq-register"/></stream:features>
08-03 06:56:26.932 8824-8863/com.example.sumit.testapp V/XMPPChatDemoActivity﹕ [SettingsDialog] Connected to 192.168.1.114
08-03 06:56:26.932 8824-8824/com.example.sumit.testapp I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@26aa149d time:22662680
08-03 06:56:26.932 8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'>=</auth>
08-03 06:56:26.932 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">cmVhbG09ImJhaGFkdXJ4eHgiLG5vbmNlPSJIVEVaUU5kRWpJM0cxRUtoWHFVYVM4akhyZkpNdTMyaTFPYVBETkJyIixxb3A9ImF1dGgiLGNoYXJzZXQ9dXRmLTgsYWxnb3JpdGhtPW1kNS1zZXNz</challenge>
08-03 06:56:26.932 8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dXNlcm5hbWU9InRlc3QiLHJlYWxtPSIxOTIuMTY4LjEuMTE0Iixub25jZT0iSFRFWlFOZEVqSTNHMUVLaFhxVWFTOGpIcmZKTXUzMmkxT2FQRE5CciIsY25vbmNlPSJVUDR5c3dpOVlMMDY3RTNvMTZrNTVwT1FKdDEzMWVSWSIsbmM9MDAwMDAwMDEscW9wPWF1dGgsZGlnZXN0LXVyaT0ieG1wcC8xOTIuMTY4LjEuMTE0IixyZXNwb25zZT04NzllZmVjZGJhMGUwMDc1N2FmZWYxZmQxOWVhYTI0MyxjaGFyc2V0PXV0Zi04</response>
08-03 06:56:26.942 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>
08-03 06:56:26.942 8824-8863/com.example.sumit.testapp V/XMPPChatDemoActivity﹕ Failed to log in as test or 192.168.1.114
08-03 06:56:26.942 8824-8863/com.example.sumit.testapp V/XMPPChatDemoActivity﹕ org.jivesoftware.smack.sasl.SASLErrorException: SASLError using DIGEST-MD5: not-authorized
08-03 06:56:30.582 8824-8824/com.example.sumit.testapp I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@26aa149d time:22666335
08-03 06:56:30.622 8824-8824/com.example.sumit.testapp E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-03 06:56:30.622 8824-8824/com.example.sumit.testapp E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-03 06:59:27.082 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <iq type="get" id="553-31" from="bahadurxxx" to="bahadurxxx/e61566be"><ping xmlns="urn:xmpp:ping"/></iq>
08-03 06:59:27.092 8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <iq to='bahadurxxx' id='553-31' type='result'></iq>
答案 0 :(得分:1)
您的客户正在尝试使用领域&#34; 192.168.1.114&#34;进行身份验证,而服务器仅提供领域&#34; bahadurxxx&#34;。那不行。您应该将SERVICE
设置为&#34; bahadurxxx&#34;或将服务器的主机名更改为&#34; 192.168.1.114&#34;。
每个XMPP地址都有一个域,例如example.com
中的user@example.com
,就像电子邮件一样。这是您必须通过XMPP验证的域以及您自己的服务器使用的地址。但是,XMPP服务器不一定需要在example.com
上运行,可能是服务器实际上在xmpp.example.com
上。 SRV记录是指示example.com
代替xmpp.example.com
的一种方式。
在Smack中,.setServiceName()
设置您要使用的XMPP域(毫不奇怪,它已被重命名为.setXmppDomain()
)。 .setHost()
可以用作另一种方式来表示您要使用xmpp.example.com
。