简体   繁体   中英

how to get openfire online registered users from android client using a Smack?

i am trying to create chat app. - I installed openfire server in my local machine - And added some users through openfire server admin panel - Now i am trying to display all available users in android Emulator. - I am able to connected to openfire server usign aSmack lib. But getting error on connection.getRoster();.

       ConnectionConfiguration config = new ConnectionConfiguration("10.42.0.3", 5222, "localhost");
        connection = new XMPPConnection(config);

        try {
            connection.connect();
            Log.d("SMACK ", " CONNECTED");


         // list online contacts
            Roster roster = connection.getRoster();
            Collection<RosterEntry> entries = roster.getEntries();
            Log.d("TRACE", "entries.size()=" + entries.size());
            for (RosterEntry e : entries) {
               Log.d("PRESENCE", e.getUser() + "=" + roster.getPresence(e.getUser()).isAvailable());
               if (roster.getPresence(e.getUser()).isAvailable()) {
                  Log.d("ADD", "NAME_KEY=" + e.getName() + " USERJID_KEY=" + e.getUser());
                  //contacts.add(contact);
               }
            }
       }
        catch (XMPPException e){
            Log.e("XMPPChatDemoActivity", "Failed to connect to "+ connection.getHost());
            Log.e("XMPPChatDemoActivity", e.toString());
        }

This code give me error on this line connection.getRoster();was not found.

here i my dependencies

compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
compile 'com.google.android.gms:play-services:8.3.0'

compile "org.igniterealtime.smack:smack-java7:4.1.0"
// Optional for XMPPTCPConnection
compile "org.igniterealtime.smack:smack-tcp:4.1.0"
// Optional for XMPP-IM (RFC 6121) support (Roster, Threaded Chats, …)
compile "org.igniterealtime.smack:smack-im:4.1.0"
// Optional for XMPP extensions support
compile "org.igniterealtime.smack:smack-extensions:4.1.0"

Smack doesn't have such feature. But you can get users list using REST API. To do this install rest api plugin in server side from plugin repository. Then in client side add rest api client ,

Maven :

<dependency>
    <groupId>org.igniterealtime</groupId>
    <artifactId>rest-api-client</artifactId>
    <version>1.1.3</version>
</dependency>

Gradle :

compile 'org.igniterealtime:rest-api-client:1.1.3'

Example :

AuthenticationToken token = new AuthenticationToken("rest_api_token");
RestApiClient client = new RestApiClient("host", port, token);
UserEntities entities = client.getUsers();    // UserEntities have all users list including admin.

** You will get rest api token in server's rest api section.

You cannot get all register user on server from roster cause a Roster is different. A roster is like individual users address book so the will be adding someone to the roster or removing from the roster.

Your XMPPClient cannot access another user details if the user is not added in the roster. Furthermore since roster is like a individuals address book so the roster generally has a different nick for a particular users jid instead of his/her name registered on server.

In Short you need to access it over http/ rest apis. You can enable rest api on your openfire admin console and you can use this rest api client for android side. - https://github.com/xibsked/Openfire-Rest-Api-Client-Android

There is an app already using this api which is like an Openfire Admin Console- https://play.google.com/store/apps/details?id=com.sked.ofadmin

Implementations is quite easy -

OfApiClient.with(this).account(account).getUser("admin",
            new Listener<User>() {
                @Override
                public void onResponse(Object mTag, User user) {
                    //Handle Success
                }
            },
            new ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //Handle Failure
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM