gpt4 book ai didi

android - 无法发送和接收 XMPP 消息

转载 作者:行者123 更新时间:2023-11-29 01:32:12 25 4
gpt4 key购买 nike

我正在尝试通过 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();
}

}
});

最佳答案

首先您需要登录到服务器。

Class Message 的链接

发送消息:我与 XMPPConnection 一起使用发送 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) {}
}
};

希望对你有帮助。

关于android - 无法发送和接收 XMPP 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30556628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com