我在将Smack库连接到Openfire服务器时收到以下错误。我在谷歌上搜索堆栈溢出,但无法解决我的问题
异常如下
Exception in thread "main" org.jivesoftware.smack.SmackException$NoResponseException
at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:352)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.login(XMPPTCPConnection.java:244)
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:442)
at JabberSmackAPI.login(JabberSmackAPI.java:64)
at JabberSmackAPI.main(JabberSmackAPI.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Process finished with exit code 1
我的代码如下
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.*;
import java.io.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.util.StringUtils;
import sun.rmi.runtime.Log;
import javax.net.ssl.*;
import javax.security.sasl.SaslException;
public class JabberSmackAPI implements MessageListener {
private XMPPConnection connection;
private final String mHost = "192.168.2.250";//if fails use "192.168.2.250" // server IP address or the
// host
public void login(String userName, String password) throws XMPPException, IOException, SmackException, NoSuchAlgorithmException, KeyManagementException {
String service = StringUtils.parseServer(userName);
final String user_name = StringUtils.parseName(userName);
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(
X509Certificate[] certs,
String authType) {
}
public void checkServerTrusted(
X509Certificate[] certs,
String authType) {
}
}
};
HostnameVerifier verifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
ConnectionConfiguration config = new ConnectionConfiguration(mHost, 5222);
SSLContext context= SSLContext.getInstance("TLS");
context.init(null, trustAllCerts, new java.security.SecureRandom());
config.setCustomSSLContext(context);
config.setHostnameVerifier(verifier);
config.setSendPresence(true);
config.setDebuggerEnabled(false);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
connection = new XMPPTCPConnection(config);
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
connection.connect();
System.out.println("JabberSmackAPI.login");
connection.login(user_name, password);
}
public void sendMessage(String msg, String message) throws XMPPException, SmackException.NotConnectedException {
ChatManager chatManager = ChatManager.getInstanceFor(connection);
Chat chat = chatManager.createChat("ali", new MyMessageListener());
chat.sendMessage(message);
}
public void displayBuddyList() {
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
System.out.println("nn" + entries.size() + " buddy(ies):");
for (RosterEntry r : entries) {
System.out.println(r.getUser());
}
}
public void disconnect() throws SmackException.NotConnectedException {
connection.disconnect();
}
public void processMessage(Chat chat, Message message) {
System.out.println("Received something: " + message.getBody());
if (message.getType() == Message.Type.chat)
System.out.println(chat.getParticipant() + " says: "
+ message.getBody());
}
public static void main(String args[]) throws XMPPException, IOException, SmackException, KeyManagementException, NoSuchAlgorithmException {
// declare variables
JabberSmackAPI c = new JabberSmackAPI();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg;
// turn on the enhanced debugger
// XMPPConnection.DEBUG_ENABLED = true;
// Enter your login information here
System.out.println("-----");
System.out.println("Login information:");
System.out.print("username: ");
String login_username = br.readLine();
System.out.print("password: ");
String login_pass = br.readLine();
c.login(login_username, login_pass);
c.displayBuddyList();
System.out.println("-----");
System.out
.println("Who do you want to talk to? - Type contacts full email address:");
String talkTo = br.readLine();
System.out.println("-----");
System.out.println("All messages will be sent to " + talkTo);
System.out.println("Enter your message in the console:");
System.out.println("-----n");
while (!(msg = br.readLine()).equals("bye")) {
c.sendMessage(msg, talkTo);
}
c.disconnect();
System.exit(0);
}
private class MyMessageListener implements MessageListener {
@Override
public void processMessage(Chat chat, Message message) {
String from = message.getFrom();
String body = message.getBody();
System.out.println(String.format("Received message " + body + " from " + from));
}
}
}
任何帮助都会通知
您有这一行:
connection.login(user_name, password);
它抛出一个无响应异常:
公共静态类SmackException.NoResponseException扩展SmackException
总是在对IQ请求没有响应时引发异常在所使用的连接实例的数据包回复超时内。
这本质上意味着你没有得到回应,所以你必须检查是否:
- 接收器存在并且能够获取和解析消息
- 接收器能够发送响应
- 发件人可以发送消息
- 连接设置正确且是最新的