我正在尝试通过Android中的SMack库发送和接收XMPP消息。但未能做同样的事情。即使我成功连接到服务器并获得在线用户。但无法发送或接收短信。请提出任何解决方案。
代码: XMPP建立连接:
try {
ConnectionConfiguration config = new ConnectionConfiguration("chat.spectratech.in");
config.setTruststorePath("/system/etc/security/cacerts.bks");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
config.setTruststoreType("AndroidCAStore");
config.setTruststorePassword(null);
config.setTruststorePath(null);
} else {
config.setTruststoreType("BKS");
String path = System.getProperty("javax.net.ssl.trustStore");
if (path == null)
path = System.getProperty("java.home") + File.separator + "etc"
+ File.separator + "security" + File.separator
+ "cacerts.bks";
config.setTruststorePath(path);
}
mXmppConnection = new XMPPConnection(config);
mXmppConnection.connect();
mXmppConnection.login(USERNAME, PASSWORD);
chatApp.setmXmppConnection(mXmppConnection);
}
catch (final XMPPException e) {
Log.e(TAG, "Could not connect to Xmpp server.", e);
return;
}
if (!mXmppConnection.isConnected()) {
Log.e(TAG, "Could not connect to the Xmpp server.");
return;
}
Log.i(TAG, "Yey! We're connected to the Xmpp server!");
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
mXmppConnection.getChatManager().addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(final Chat chat, final boolean createdLocally) {
if (!createdLocally) {
chat.addMessageListener(new MyMessageListener());
}
}
});
发送和接收消息:
if (app.getmXmppConnection() != null) {
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
app.getmXmppConnection().addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message
.getFrom());
Log.i("XMPPChatDemoActivity", "Text Recieved "
+ message.getBody() + " from " + fromName);
msgs.add(fromName + ": " + message.getBody());
//messages.add(fromName + ":");
//messages.add(message.getBody());
// Add the incoming message to the list view
mHandler.post(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
}
});
}
}
}, filter);
}
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String text = edMsgs.getText().toString();
Message msg = new Message(to, Message.Type.chat);
msg.setBody(text);
if(app.getmXmppConnection()!=null){
app.getmXmppConnection().sendPacket(msg);
msgs.add(text);
adapter.notifyDataSetChanged();
}
}
});
答案 0 :(得分:1)
首先,您需要登录服务器。
的链接发送消息: 我使用XMPPConnection Class发送sendPacket。
String message = "Hello friend";
Message msg = new Message(toUserId, Message.Type.chat);
msg.setBody(message);
connection.sendPacket(msg);
接收讯息:
我使用PacketFilter类来仅回放聊天消息。 在消息提交时我使用XMPPConnection类添加列表器。
PacketFilter chatFilter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(chatPacketListener, chatFilter);
PacketListener chatPacketListener = new PacketListener() {
@Override
public void processPacket(Packet packet) {
try {
Message message = (Message) packet;
String body = message.getBody();
String from = StringUtils.parseBareAddress(message.getFrom());
} catch (Exception e) {}
}
};
我希望它可以帮到你。