Presence is incorrect and can't send message

I have a problem getting the presence of the other user with code. There is nothing wrong with the users configuration and the openfire server as i’ve tested everything with the SPARK client application.

on the line below

System.out.println(entry + " is " + roster.getPresence(“userB”));

I always get unavailable despite userB being online. Also I cannot send a chat message.

I’ve also tried adding a Thread sleep(5000) after connecting as suggested in another forum but to no avail. Any ideas why this isn’t working?

I’m using the smack API and my code below is as follows

package com.order.xmpp; import java.io.IOException;
import java.util.Calendar;
import java.util.Collection; import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence; public class SendTestOpenFire {     public static void main(String[] args) {         System.out.println("Starting IM client");         // gtalk requires this or your messages bounce back as errors
        ConnectionConfiguration connConfig = new ConnectionConfiguration("MY_SERVER", 5222);
        XMPPConnection connection = new XMPPConnection(connConfig);         try {
            connection.connect();
            System.out.println("Connected to " + connection.getHost());
        } catch (XMPPException ex) {
            //ex.printStackTrace();
            System.out.println("Failed to connect to " + connection.getHost());
            System.exit(1);
        }
        try {
            connection.login("userA", "userA");             System.out.println("Logged in as " + connection.getUser());             Presence presence = new Presence(Presence.Type.available);
            presence.setMode(Presence.Mode.available);
            connection.sendPacket(presence);             //getRoster
            Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.accept_all);
            Roster roster = connection.getRoster();
            Collection<RosterEntry> entries = roster.getEntries();
            for (RosterEntry entry : entries) {
                System.out.println(entry + " is " + roster.getPresence("userB"));
                System.out.println();
            }         } catch (XMPPException ex) {
            //ex.printStackTrace();
            System.out.println("Failed to log in as " + connection.getUser());
            System.exit(1);
        }         ChatManager chatmanager = connection.getChatManager();         /*//Add chat  listener
        chatmanager.addChatListener(new ChatManagerListener() {
            @Override
            public void chatCreated(Chat chat, boolean b) {
                chat.addMessageListener(new MessageListener() {
                    @Override
                    public void processMessage(Chat chat, Message message) {
                        System.out.println("Received chat message " + (message != null ? message.getBody() : "NULL"));
                    }
                });
            }
        });*/         Chat chat = chatmanager.createChat("userB", new MessageListener() {
            @Override
            public void processMessage(Chat chat, Message message) {
                System.out.println("Received Message: " + message.getBody() + " | " + message.getSubject());
            }
        });         try {
            // google bounces back the default message types, you must use chat
            System.out.println("SENDING MESSAGE!!!!");
            Message msg = new Message("userB", Message.Type.normal);
            msg.setSubject("TEST SUBJECT");
            msg.setBody("SOME BODY");
            chat.sendMessage(msg);
        } catch (XMPPException e) {
            System.out.println("Failed to send message");
            // handle this how?
        }         System.out.println("Press enter to disconnect");         try {
            System.in.read();
        } catch (IOException ex) {
            //ex.printStackTrace();
        }         connection.disconnect();
    } }

So everyone here has had 100% success with code like this?

I am connecting to Facebook and it is showing me wrong presence…