lastActivity returning (-1) in Smack 4.1

I was using the following code, when using asmack library and it was working fine

LastActivity lastActivity = LastActivityManager.getLastActivity(connection, occupantJID);
int seconds = lastActivity.lastActivity;

In Smack 4.1, I am using the following code, but I am getting seconds = -1

LastActivity lastActivity = new LastActivity(occupantJID);
int seconds = lastActivity.lastActivity;

Why LastActivity is not working in Smack4.1?

I found out the solution, if someone is looking for, here is the code.

LastActivity iq = new LastActivity();
iq.setTo(occupantJID);
StanzaListener callback = new StanzaListener() {
    @Override
    public void processPacket(Stanza packet) throws NotConnectedException {
        LastActivity lastActivity = (LastActivity)packet;
        int seconds = lastActivity.lastActivity;
    }
};
connection.sendIqWithResponseCallback(iq, callback);

In Smack 4.1, I am using the following code, but I am getting seconds = -1

  1. LastActivity lastActivity = new LastActivity(occupantJID);
  2. int seconds = lastActivity.lastActivity;

That creates a LastActivity IQ request stanza and reads out the value of lastActivity which is per default initialized to ‘-1’. You should use LastActivityManager (Smack 4.1.1 API) getLastActivity() instead.

What server are you running with this?

I am running Ejabbered