使用JavaSMACKAPI发送和接收消息在示例中不起作用
我仍在努力学习如何正确使用java Smack API,因此我在java编程论坛上学习了一个小型教程:使用JavaSMACKAPI发送和接收消息在示例中不起作用,java,eclipse,xmpp,smack,Java,Eclipse,Xmpp,Smack,我仍在努力学习如何正确使用java Smack API,因此我在java编程论坛上学习了一个小型教程: 然后我根据自己的需要修改了代码。代码发布如下: import java.util.*; import java.io.*; import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.MessageList
import java.util.*;
import java.io.*;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
public class JabberSmackAPI implements MessageListener{
private XMPPConnection connection;
public void login(String userName, String password) throws XMPPException
{
ConnectionConfiguration config = new ConnectionConfiguration("localhost", 5222);
connection = new XMPPConnection(config);
connection.connect();
connection.login(userName, password);
}
public void sendMessage(String message, String to) throws XMPPException
{
Chat chat = connection.getChatManager().createChat(to, this);
chat.sendMessage(message);
}
public void displayBuddyList()
{
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
System.out.println("\n\n" + entries.size() + " buddy(ies):");
for(RosterEntry r:entries)
{
System.out.println(r.getUser());
}
}
public void disconnect()
{
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
{
// 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);
}
}
import java.util.*;
导入java.io.*;
导入org.jivesoftware.smack.Chat;
导入org.jivesoftware.smack.ConnectionConfiguration;
导入org.jivesoftware.smack.MessageListener;
导入org.jivesoftware.smack.floster;
导入org.jivesoftware.smack.rosternetry;
导入org.jivesoftware.smack.XMPPConnection;
导入org.jivesoftware.smack.XMPPException;
导入org.jivesoftware.smack.packet.Message;
公共类JabberSmackAPI实现MessageListener{
专用XMPPConnection连接;
公共无效登录(字符串用户名、字符串密码)抛出XMPPException
{
ConnectionConfiguration config=新的ConnectionConfiguration(“本地主机”,5222);
连接=新的XMPPConnection(配置);
connection.connect();
连接。登录(用户名、密码);
}
public void sendMessage(String message,String to)抛出XMPPException
{
Chat Chat=connection.getChatManager().createChat(to,this);
发送消息(message);
}
public void displaybudylist()
{
花名册=connection.get花名册();
集合条目=花名册.getEntries();
System.out.println(“\n\n”+entries.size()+“buddy:”);
对于(名册r:条目)
{
System.out.println(r.getUser());
}
}
公共空间断开连接()
{
连接断开();
}
public void processMessage(聊天室、消息消息)
{
System.out.println(“收到某物:+message.getBody());
if(message.getType()==message.Type.chat)
System.out.println(chat.getParticipant()+”表示:“+message.getBody());
}
公共静态void main(字符串args[])抛出xmppeexception、IOException
{
//声明变量
JabberSmackAPI c=新的JabberSmackAPI();
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
串味精;
//打开增强的调试器
XMPPConnection.DEBUG_ENABLED=true;
//在此处输入您的登录信息
System.out.println(“----”);
System.out.println(“登录信息:”);
系统输出打印(“用户名:”);
字符串login_username=br.readLine();
系统输出打印(“密码:”);
字符串login_pass=br.readLine();
c、 登录(登录用户名、登录密码);
c、 displaybudylist();
System.out.println(“----”);
System.out.println(“你想和谁通话?-键入联系人完整电子邮件地址:”);
String talkTo=br.readLine();
System.out.println(“----”);
System.out.println(“所有消息将发送到”+talkTo);
System.out.println(“在控制台中输入消息:”);
System.out.println(“----\n”);
而(!(msg=br.readLine()).equals(“再见”))
{
c、 发送消息(msg、talkTo);
}
c、 断开连接();
系统出口(0);
}
}
为了运行此代码,我有以下设置:
但是,它们也适用于特定情况,似乎对我没有帮助,因为我希望此示例能够正常运行。我做错了什么?@Flame\u Phoenix您的示例代码正在运行,我与我的ejabberd服务器进行了检查。我修改了密码 确保已添加这些库
- httpclient-4.1.3.jar
- httpcore-4.1.4.jar
- jstun.jar
- xpp3-1.1.4c.jar
import java.util.*;
import java.io.*;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.StringUtils;
public class JabberSmackAPI implements MessageListener {
private XMPPConnection connection;
private final String mHost = "yourserver.com"; // server IP address or the
// host
public void login(String userName, String password) throws XMPPException {
String service = StringUtils.parseServer(userName);
String user_name = StringUtils.parseName(userName);
ConnectionConfiguration config = new ConnectionConfiguration(mHost,
5222, service);
config.setSendPresence(true);
config.setDebuggerEnabled(false);
connection = new XMPPConnection(config);
connection.connect();
connection.login(user_name, password);
}
public void sendMessage(String message, String to) throws XMPPException {
Chat chat = connection.getChatManager().createChat(to, this);
chat.sendMessage(message);
}
public void displayBuddyList() {
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
System.out.println("\n\n" + entries.size() + " buddy(ies):");
for (RosterEntry r : entries) {
System.out.println(r.getUser());
}
}
public void disconnect() {
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 {
// 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);
}
}
import java.util.*;
导入java.io.*;
导入org.jivesoftware.smack.Chat;
导入org.jivesoftware.smack.ConnectionConfiguration;
导入org.jivesoftware.smack.MessageListener;
导入org.jivesoftware.smack.floster;
导入org.jivesoftware.smack.rosternetry;
导入org.jivesoftware.smack.XMPPConnection;
导入org.jivesoftware.smack.XMPPException;
导入org.jivesoftware.smack.packet.Message;
导入org.jivesoftware.smack.util.StringUtils;
公共类JabberSmackAPI实现MessageListener{
专用XMPPConnection连接;
私有最终字符串mHost=“yourserver.com”;//服务器IP地址或
//主人
公共无效登录(字符串用户名、字符串密码)抛出XMPPException{
String service=StringUtils.parseServer(用户名);
字符串user\u name=StringUtils.parseName(用户名);
ConnectionConfiguration配置=新的ConnectionConfiguration(mHost,
5222(服务);
config.setSendPresence(true);
config.setDebuggerEnabled(false);
连接=新的XMPPConnection(配置);
connection.connect();
连接。登录(用户名、密码);
}
public void sendMessage(String message,String to)抛出XMPPException{
Chat Chat=connection.getChatManager().createChat(to,this);
发送消息(message);
}
public void displaybudylist(){
花名册=connection.get花名册();
集合条目=花名册.getEntries();
系统
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smackx</artifactId>
<version>3.3.1</version>
</dependency>
<repository>
<id>nexus.opencastProject.org</id>
<url>http://repository.opencastproject.org/nexus/content/repositories/public</url>
<name>Opencast Nexus All Public Project Repository</name>
</repository>