get online users(Roster entries) using smack 4.1 in android
Asked Answered
W

2

7

I have been trying to get roster entries using smack 4.1 beta 2 in android.

https://github.com/igniterealtime/Smack/wiki/Smack-4.1-Readme-and-UpgradeGuide Quotes from above link..

"Roster now follows the Manager pattern (use Roster.instanceFor to obtain an instance, no more XMPPConnection.getRoster)"

First of all i am not able get the "Roster" object, libraries that i have imported may not have that package or i miss any lib here?

I m using all the libraries mentioned in above link.

Can anyone help me to get the Roster Entries using smack 4.1?

Thanks

Wiggle answered 17/2, 2015 at 10:24 Comment(1)
I think the link is wrongAntionetteantioxidant
C
22

This is a step-by-step solution that concludes by (hopefully) answering your question. You should pay particular attention to the Java imports in STEP 2, and the Roster.reloadAndWait() method in STEP 4.

NOTE: It is recommended that Smack code be executed using AsyncTask.

Step 1: Include the following dependencies. For Android Studio users, this is located in 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" 
}

Also ensure your program has proper permissions for TCP activity. For Android Studio users, you may add this to your AndroidManifest.xml file:

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

Step 2: Import the following

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*/

Step 3: Connect to server

/*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");
...

Step 4: Get the roster

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

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

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

for (RosterEntry entry : entries) 
    System.out.println("Here: " + entry);
Carleecarleen answered 3/3, 2015 at 8:20 Comment(5)
i cant retrive rosters with presense. every time i get null status as well as unavailable in presense type.Friend
I get 0 entry after successful login.Buie
Hi, i am getting 0 entries, what could be the issue?Ignatius
This is not working, it is always giving me 0 entries. What could be wrong?Ignatius
you need to add roaster entry (using roster.createEntry()) to your Roaster then only it will list the entries. Otherwise 0 as no entries are added.Nev
V
0

You can use Smack-xxx-4.1.0-rc5 like below:

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
Vallation answered 30/3, 2015 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.