AnsweredAssumed Answered

Create a group chat like whatsapp?

Question asked by Hitendrasinh on Mar 19, 2015
Latest reply on Jul 24, 2015 by Vilpesh

Hi,

 

I am using smack android rc1. I want to create group chat same as whatsapp. I am confused if i can do it by muc or create group.

 

I am able to create room with below code.

try {

            multiUserChat.create(roomName);

            Form form = multiUserChat.getConfigurationForm();

            Form submitForm = form.createAnswerForm();

            for (FormField formField : form.getFields()) {

                if (FormField.Type.hidden.equals(form.getType()) && formField.getVariable() != null) {

                    submitForm.setDefaultAnswer(formField.getVariable());

                }

            }

            List<String> ownersList = new ArrayList<String>();

            ownersList.add(user + "@" + ChatController.HOST);

            submitForm.setAnswer("muc#roomconfig_publicroom", true);

            multiUserChat.sendConfigurationForm(submitForm);

            multiUserChat.join(user);

            multiUserChat.join(user1);

            multiUserChat.join(user2);

            List<String> joinedRoomes = multiUserChatManager.getJoinedRooms(xmpptcpConnection.getUser());

            for (String jRoomName : joinedRoomes) {

                Log.i(Constant.TAG, "Joined room = " + jRoomName);

            }

        } catch (XMPPException.XMPPErrorException e) {

            e.printStackTrace();

        } catch (SmackException e) {

            e.printStackTrace();

        }

 

 

 

Create group :-

 

 

    public void addRosterEntry(String jid, String nickname, String group) {

        final Roster roster = Roster.getInstanceFor(xmpptcpConnection);

        String[] groups = {group};

        RosterEntry userEntry = roster.getEntry(jid);

        boolean isSubscribed = true;

        if (userEntry != null) {

            isSubscribed = userEntry.getGroups().isEmpty();

        }

        if (isSubscribed) {

            try {

                roster.createEntry(jid, nickname, groups);

            } catch (SmackException.NotLoggedInException e) {

                e.printStackTrace();

            } catch (SmackException.NoResponseException e) {

                e.printStackTrace();

            } catch (XMPPException.XMPPErrorException e) {

                e.printStackTrace();

            } catch (SmackException.NotConnectedException e) {

                e.printStackTrace();

            }

            RosterGroup rosterGroup = roster.getGroup(group);

            if (rosterGroup == null) {

                rosterGroup = roster.createGroup(group);

                if (userEntry == null) {

                    try {

                        roster.createEntry(jid, nickname, groups);

                        userEntry = roster.getEntry(jid);

                    } catch (SmackException.NotLoggedInException e) {

                        e.printStackTrace();

                    } catch (SmackException.NoResponseException e) {

                        e.printStackTrace();

                    } catch (XMPPException.XMPPErrorException e) {

                        e.printStackTrace();

                    } catch (SmackException.NotConnectedException e) {

                        e.printStackTrace();

                    }

                } else {

                    try {

                        userEntry.setName(nickname);

                        rosterGroup.addEntry(userEntry);

                    } catch (SmackException.NoResponseException e) {

                        e.printStackTrace();

                    } catch (XMPPException.XMPPErrorException e) {

                        e.printStackTrace();

                    } catch (SmackException.NotConnectedException e) {

                        e.printStackTrace();

                    }

                }

                userEntry = roster.getEntry(jid);

            }

        }

    }

 

 

After creating group i am trying to get it by roster.getGroups() but i am not able to get created group.

 

My requirement is to create group, add contacts to that group and people can chat in that group.

 

So can anybody please give me some idea to achieve it.

 

Thanks

Outcomes