将XMPP数据包发送到Android上的Openfire插件

时间:2012-08-10 18:01:56

标签: android xmpp send packet openfire

我正在尝试实现一个简单的Android应用程序,该应用程序向为Openfire服务器编写的插件发送和接收数据包。该插件旨在从客户端接收数据包以进行进一步处理。所以这不是聊天。以下代码段显示了我向服务器发送数据包的方式:

ConnectionConfiguration configuration = new ConnectionConfiguration(
        HOST, PORT);
Connection connection = new XMPPConnection(configuration);
try {
    connection.connect();
} catch (XMPPException e) {
    e.printStackTrace();
}
if (connection.isConnected()) {
    Packet packet = new Message();
    packet.setFrom("123456789@localhost");
    packet.setTo("987654321@component.localhost");
    connection.sendPacket(packet);
    connection.disconnect();
}

HOST和PORT是预定义的常量。

我尝试在插件中使用if子句中的代码并且它完美地工作 - 组件接收数据包并与它们一起工作。但是,在我的Android应用程序中,此代码不起作用 - 数据包无法到达组件。

所以,伙计们,如果您有任何建议,我会很乐意为您提供帮助。也许我在某处使用了错误的技术 - 我是XMPP和Openfire的新手。


更新

应用程序清单中有所有必需的权限。 HOST等于运行Openfire服务器的PC的静态IP地址。

private static final String HOST = "192.168.1.100";
private static final int PORT = 5222;

2 个答案:

答案 0 :(得分:0)

要将数据包发送到服务器,您应使用login()类的loginAnonymously()org.jivesoftware.smack.Connection方法登录到该服务器。

谢谢先生。流程为提示。

答案 1 :(得分:0)

连接和断开连接

// Create the configuration for this new connection
ConnectionConfiguration config = new ConnectionConfiguration("jabber.org", 5222);
config.setCompressionEnabled(true);
config.setSASLAuthenticationEnabled(true);

Connection connection = new XMPPConnection(config);
// Connect to the server
connection.connect();
// Log into the server
connection.login("username", "password", "SomeResource");
....
// Disconnect from the server
connection.disconnect();
</code>

您应首先登录,以下是管理联系http://www.igniterealtime.org/builds/smack/docs/latest/documentation/connections.html

的指南