在 xmpp smack 库中收到两次消息
Posted
技术标签:
【中文标题】在 xmpp smack 库中收到两次消息【英文标题】:Message is received two times in xmpp smack library 【发布时间】:2016-10-14 13:40:41 【问题描述】:我正在使用 xmpp smack 库创建一个 android 聊天应用程序。我创建了一个后台服务来监听传入的聊天,并使用一个应用程序类来初始化 xmpp 连接对象。 问题是聊天侦听器的 chatCreated 函数被调用了两次,因此显示了重复的消息。 这是我创建连接的应用程序类
验证.java
public class Authenticate extends Application
private static final String DOMAIN = StaticVariables.chatServer;
private static final String HOST = StaticVariables.chatServer;
private static final int PORT = 5222;
static AbstractXMPPConnection connection ;
String username,password;
private boolean connected;
@Override
public void onCreate()
super.onCreate();
public AbstractXMPPConnection initializeXMPPTCPConnection(String username,String password)
Log.e("APPLICATION", "username: "+username);
Log.e("APPLICATION", "password: "+password);
Log.i("APPLCATION", "initializeXMPPTCPConnection calle:");
this.username=username;
this.password=password;
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword(username, password);
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
configBuilder.setResource("Android");
configBuilder.setServiceName(DOMAIN);
configBuilder.setHost(HOST);
configBuilder.setPort(PORT);
configBuilder.setDebuggerEnabled(true);
connection = new XMPPTCPConnection(configBuilder.build());
connection=connectConnection();
Log.e("APPLICATION", "initializeXMPPTCPConnection: "+connection.isConnected());
return connection;
public AbstractXMPPConnection getConnection()
return connection;
public AbstractXMPPConnection connectConnection()
AsyncTask<Void, Void, AbstractXMPPConnection> connectionThread = new AsyncTask<Void, Void, AbstractXMPPConnection>()
@Override
protected AbstractXMPPConnection doInBackground(Void... arg0)
// Create a connection
try
connection.connect().login();
Log.e("Application", "doInBackground: "+connection.isConnected());
//login();
connected = true;
//sendMsg();
catch (IOException e)
e.printStackTrace();
catch (SmackException e)
e.printStackTrace();
catch (XMPPException e)
e.printStackTrace();
return connection;
@Override
protected void onPostExecute(AbstractXMPPConnection connection2)
super.onPostExecute(connection2);
// sendMsg(message.getText().toString());
Log.e("APPLICATION", "onPostExecute: "+connection2.isConnected());
connection=connection2;
;
try
connection=connectionThread.execute().get();
catch (InterruptedException e)
e.printStackTrace();
catch (ExecutionException e)
e.printStackTrace();
Log.e("Application", "connectConnection: "+connection.isConnected());
return connection;
public void login()
try
connection.login(username, password);
Log.e("APPLICATIPN", "Yey! We're connected to the Xmpp server!");
catch (XMPPException | SmackException | IOException e)
e.printStackTrace();
catch (Exception e)
这是我调用连接实例的代码
connection=((Authenticate)getApplication()).getConnection();
监听函数
public void listenChat(String name)
ChatManager manager = ChatManager.getInstanceFor(connection);
manager.addChatListener(new ChatManagerListener()
@Override
public void chatCreated(final Chat chat, boolean createdLocally)
System.out.println("Created chat");
chat.addMessageListener(new ChatMessageListener()
@Override
public void processMessage(final Chat chat, final org.jivesoftware.smack.packet.Message message)
//This is called twice
);
);
我是 android 和 xmpp 的初学者。请告诉我我哪里错了如何解决它
【问题讨论】:
只是一个建议,不要将您的主登录名放在应用程序类中。创建一个单独的单例。你的connectConnection、listenChat和initializeXMPPConnection在哪里被调用? 确定调用了多少次listenChat()这个方法,我觉得是调用了两次所以... 【参考方案1】:您必须在 chatCreated 方法中修复 createdLocally 用法。
如果没有像这样在本地创建,只需添加监听器:
public void chatCreated(Chat chat, boolean createdLocally)
if (!createdLocally)
chat.addMessageListener(new ChatMessageListener()...);
【讨论】:
以上是关于在 xmpp smack 库中收到两次消息的主要内容,如果未能解决你的问题,请参考以下文章
使用 Smack API (xmpp) 从 Java 回调 JavaFX
无法在 JAVA 中使用 SMACK api 显示收到的消息
我们如何在 smack 或来自 android 的 xmpp 中获得对发件人的消息读取/看到确认