1

I'm working on an Android app that connects to an Openfire server using the Smack library. I have an activity that calls an AsyncTask from the onStart() method to retrieve information and add it to the UI.

The AsyncTask will use the getHostedRooms() method from the Smack MultiUserManager class, loop through each room and get the JID value and the subject string from the room, adding that information to a hash map. That hash map is then returned to the main thread in the onPostExecute method.

The problem I have is that the async task often does not provide all the correct values. For example, there are three rooms, often it will only return information for one or two, sometimes sending null instead of the subject string.

When I debug the code and go through it step by step, it works every time, so I think the problem is that the asynctask is not being allowed to call every step in the loop before it returns when running at full speed.

Is there any way that I can ensure that the async task is allowed to complete and return as expected? I have looked at the asynctask.get() method, but this appears to block the UI thread. I've copied the AsyncTask below, please let me know if you need more information!

Edit: Just to clarify, all key values being entered into the hash map are different each time

private class GetChatRoomData extends AsyncTask<Void, Void, HashMap<String, String>> {
    private Context context;
    private String username, password, returnMessage;
    private XMPPTCPConnection connection;
    private GetSubjectsCallBack callBack;

    private GetChatRoomData(User user, GetSubjectsCallBack callBack, Context context) {
        this.context = context;
        this.callBack = callBack;
        username = user.getUsername(); password = user.getPassword();
        returnMessage = "Subjects found";
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog.show();
    }

    @Override
    protected HashMap<String, String> doInBackground(Void... params) {
        XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                .setUsernameAndPassword(username, password)
                .setServiceName(myservicename)
                .setHost("10.0.2.2")
                .setPort(5222).setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .build();

        HashMap<String, String> returnmap = new HashMap<String, String>();

        try {
            connection = new XMPPTCPConnection(config);
            connection.setPacketReplyTimeout(10000);
            connection.connect();
            connection.login(username, password);

            MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
            List<HostedRoom> list = manager.getHostedRooms(myservicename);

            if(list.size() != 0) {
                for (HostedRoom room : list) {
                    String jid = room.getJid();
                    MultiUserChat tempMuc =
                            manager.getMultiUserChat(jid);
                    if (!(tempMuc.isJoined())) {
                        tempMuc.join(username);
                    }
                    String subject = tempMuc.getSubject();
                    returnmap.put(subject, jid);
                }
            } else {
                returnMessage = "No topics created yet";
            }

            connection.disconnect();

        } catch (Exception e) {
            returnMessage = "Could not connect";
        }

            return returnmap;
    }

    @Override
    protected void onPostExecute(HashMap<String, String> map) {
        progressDialog.dismiss();
        callBack.done(returnMessage, map);
        super.onPostExecute(map);
    }
}
4
  • Maybe there is more than a value with the same key "subject's value" in your HashMap, so the loop overwrites the values.
    – SaNtoRiaN
    Aug 6, 2015 at 1:13
  • @SaNtoRiaN Cheers for suggestion, have already checked this though and it isn't the case, each room has a different subject Aug 6, 2015 at 1:20
  • by the way, move the part super.onPostExecute(map); to the first line of the method onPostExecute
    – SaNtoRiaN
    Aug 6, 2015 at 1:27
  • Thanks. Still not working but good to know regardless. Aug 6, 2015 at 1:35

1 Answer 1

0

I found the solution, will post it here for future use. Turns out the problem was with the call to join() within the for loop. Sometimes the connection would not be made before the call to getSubject(), and when this happened subject returned as null. If this happened multiple times then multiple values would be put in with key 'null', thus returning an incomplete hash map.

The solution I currently have is just to call Thread.sleep() after join() to ensure the connection is made. If the connection cannot be made an exception is thrown. Possibly not the best solution, but it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.