1
votes

I am making a chat application with smack library and openfire as a server but everytime i exit the chat conversation activity between two users and come back, the whole chat gets erased. I have already enabled archive settings to store one to one messages in the server but i do not know how to implement it in the app.

I want to show chats history in recyclerview by the sender and the receiver in the recyclerview.

currently i have implemented this function which caused error

private void setChatHistory(String entityBareId) {

    EntityBareJid jid = null;
    try {
        jid = JidCreate.entityBareFrom(entityBareId);
    } catch (XmppStringprepException e) {
        e.printStackTrace();
    }

    MamManager manager = MamManager.getInstanceFor(mConnection);
    MamManager.MamQueryResult r = null;
    try {
        try {
            r = manager.mostRecentPage(jid, 10);
        } catch (SmackException.NotConnectedException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } catch (XMPPException.XMPPErrorException e) {
        e.printStackTrace();
    } catch (SmackException.NotLoggedInException e) {
        e.printStackTrace();
    } catch (SmackException.NoResponseException e) {
        e.printStackTrace();
    }
    if (r.forwardedMessages.size() >= 1) //printing first of them
    {
        Message message = (Message) r.forwardedMessages.get(0).getForwardedStanza();
        Log.i("mam", "message received" + message.getBody());

        MessagesData data = new MessagesData("send",message.getBody());
        mMessagesData.add(data);
        mAdapter = new ConversationAdapter(mMessagesData);
        recyclerView.setAdapter(mAdapter);

    }

}

Error was

Attempt to read from field 'java.util.List org.jivesoftware.smackx.mam.MamManager$MamQueryResult.forwardedMessages' on a null object reference

At r.forwardedmessages.size()>=1.

Thanks in advance

1

1 Answers

1
votes

If you want to keep history of conversation, you must save them in database. MAM just for fetching old conversation from server like when you uninstall or logout the app and decide to reinstall and get old messages. For getting messages from server be sure you already enabled it, then forwarded messages shouldnt be null. here is a guide to enable it.