【问题标题】:get online users(Roster entries) using smack 4.1 in android在 android 中使用 smack 4.1 获取在线用户(名册条目)
【发布时间】:2015-04-18 01:05:43
【问题描述】:

我一直在尝试在 android 中使用 smack 4.1 beta 2 获取名册条目。

https://github.com/igniterealtime/Smack/wiki/Smack-4.1-Readme-and-UpgradeGuide 来自以上链接的引用..

“Roster 现在遵循 Manager 模式(使用 Roster.instanceFor 获取实例,不再使用 XMPPConnection.getRoster)”

首先我无法获得“名册”对象,我导入的库可能没有那个包或者我错过了这里的任何库?

我正在使用上面链接中提到的所有库。

谁能帮助我使用 smack 4.1 获取名册条目?

谢谢

【问题讨论】:

  • 我认为链接错误

标签: android xmpp smack asmack


【解决方案1】:

这是一个循序渐进的解决方案,最后(希望)回答您的问题。您应该特别注意 STEP 2 中的 Java 导入,以及 STEP 4 中的 Roster.reloadAndWait() 方法。

注意:建议使用AsyncTask 执行 Smack 代码。

第 1 步:包括以下依赖项。对于 Android Studio 用户,它位于 build.gradle (Module:app)

dependencies {
    compile "org.igniterealtime.smack:smack-android:4.1.0-rc1"
    compile "org.igniterealtime.smack:smack-android-extensions:4.1.0-rc1"
    compile "org.igniterealtime.smack:smack-tcp:4.1.0-rc1" 
}

还要确保您的程序对 TCP 活动具有适当的权限。对于 Android Studio 用户,您可以将其添加到您的 AndroidManifest.xml 文件中:

<uses-permission android:name="android.permission.INTERNET"/>

第 2 步:导入以下内容

import org.jivesoftware.smack.roster.*; /*you may have been missing this*/
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.tcp.*;
import java.util.Collection; /*optional*/

第 3 步:连接到服务器

/*Example solution. The exact settings would have to be adjusted outside  of practice*/
XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration
    .builder()
    .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
    .setServiceName("192.168.2.14")
    .setHost("192.168.2.14")
    .setPort(5222)
    .setCompressionEnabled(false).build();
    XMPPTCPConnection connection = new XMPPTCPConnection(conf);

try {
    connection.connect();
    connection.login("john","123");
...

第 4 步:获取名册

...
Roster roster = Roster.getInstanceFor(connection);

if (!roster.isLoaded()) 
    roster.reloadAndWait();

Collection <RosterEntry> entries = roster.getEntries();

for (RosterEntry entry : entries) 
    System.out.println("Here: " + entry);

【讨论】:

  • 我无法通过 presense 检索名册。每次我得到 null 状态以及 presense 类型不可用时。
  • 登录成功后得到0个条目。
  • 嗨,我收到 0 个条目,可能是什么问题?
  • 这不起作用,它总是给我 0 个条目。有什么问题?
  • 您需要向您的 Roaster 添加烘焙器条目(使用 roster.createEntry()),然后只有它会列出条目。否则为 0,因为没有添加条目。
【解决方案2】:

您可以使用Smack-xxx-4.1.0-rc5,如下所示:

smack-android-4.1.0-rc5.jar
smack-android-extensions-4.1.0-rc5.jar
smack-core-4.1.0-rc5.jar
smack-experimental-4.1.0-rc5.jar
smack-extensions-4.1.0-rc5.jar
smack-im-4.1.0-rc5.jar
smack-resolver-minidns-4.1.0-rc5.jar
smack-sasl-provided-4.1.0-rc5.jar
smack-tcp-4.1.0-rc5.jar

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 2015-01-13
    • 2015-04-22
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多