sending and receiving message through xmpp on andr

2019-09-06 11:15发布

问题:

I am a beginner for chatting app. I searched and studied xmpp servers. I installed openfire xmpp server. The connection with xmpp server, registration and login tasks are done. Now i am trying to send and receive message. After user gets login and after that as user enters text to send. I used this code to send message

    ChatManager chatmanager;
    chatmanager = connection.getChatManager();
    Chat newChat = chatmanager.createChat("cac6ba9dc9c6ac67"+"@pc", FirstPage.this);

                try {
                    newChat.sendMessage("Howdy!");
                }
                catch (XMPPException e) {
                    System.out.println("Error Delivering block");
                }

@Override
    public void processMessage(Chat chat, Message message) {
        // TODO Auto-generated method stub
        Toast.makeText(FirstPage.this,"mssg: "+message.getBody(), Toast.LENGTH_SHORT).show();
        switch (message.getType())
        {
        case chat:
            String jsonData = (null==message.getBody())?"":message.getBody();

            System.out.println(jsonData);
            System.out.println(chat.getParticipant());
            break;
        case error:
            break;
        case groupchat:
            break;
        case headline:
            break;
        case normal:

            break;
        }
    }

and this code I am using for receiving message, kindly see over this screenshot to see users,

chatmanager.addChatListener(this);
        PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
        connection.addPacketListener(this, filter);

@Override
    public void chatCreated(Chat chat, boolean createdlocally) {
        // TODO Auto-generated method stub
        // System.out.println("1 message received "+chat.getParticipant());

        if (!createdlocally) {
            System.out.println("not created locally");
            Toast.makeText(FirstPage.this, "not created locally", Toast.LENGTH_SHORT).show();
            chat.addMessageListener(new MessageListener() {

                @Override
                public void processMessage(Chat arg0, Message arg1) {
                    // TODO Auto-generated method stub
                    //tv1.setText(arg1.getBody());
                }
            });
        } else {
            Toast.makeText(FirstPage.this, "created locally", Toast.LENGTH_SHORT).show();
            System.out.println("created locally");
        }
    }

    @Override
    public void processPacket(Packet packet) {
        // TODO Auto-generated method stub
        System.out.println("in processPacket");
        Message message = (Message) packet;
        String body = message.getBody();
        String from = message.getFrom();
        tv1.setText("From "+from + "\n"+ body);
    }

I am using this code don't know why the message is not sending over other side Can anyone please tell what mistake I am making or help in solving this problem

回答1:

I found this solution for this:

For sending message

private ArrayList<String> messages = new ArrayList<String>();
    private Handler mHandler = new Handler();
@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btnsendmsg:

            String to = "cac6ba9dc9c6ac67@pc";

            String text = edgetmsg.getText().toString();
            Message msg = new Message(to, Message.Type.chat);
            msg.setBody(text);
            if (connection != null) {
                connection.sendPacket(msg);
                messages.add(connection.getUser() + ":  " + text);
                //messages.add(connection.getUser() + ":");
                //messages.add(text);
                Log.v("text send", connection.getUser().toString());
                Toast.makeText(FirstPage.this, "txt send",
                        Toast.LENGTH_SHORT).show();
                setListAdapter();
                Roster roster = connection.getRoster();
                Collection<RosterEntry> entries = roster.getEntries();
                for (RosterEntry entry : entries) {
                    Log.d("XMPPChatDemoActivity",
                            "--------------------------------------");
                    Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
                    Log.d("XMPPChatDemoActivity",
                            "User: " + entry.getUser());
                    Log.d("XMPPChatDemoActivity",
                            "Name: " + entry.getName());
                    Log.d("XMPPChatDemoActivity",
                            "Status: " + entry.getStatus());
                    Log.d("XMPPChatDemoActivity",
                            "Type: " + entry.getType());
                    Presence entryPresence = roster.getPresence(entry
                            .getUser());
                    Log.d("XMPPChatDemoActivity", "Presence Status: "
                            + entryPresence.getStatus());
                    Log.d("XMPPChatDemoActivity", "Presence Type: "
                            + entryPresence.getType());
                    Presence.Type type = entryPresence.getType();
                    if (type == Presence.Type.available)
                        Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
                    Log.d("XMPPChatDemoActivity", "Presence : "
                            + entryPresence);
                    // entryPresence.setPriority(1);
                    // connection.sendPacket(entryPresence);
                    // Toast.makeText(XMPPChatDemoActivity.this,
                    // "gettting user", Toast.LENGTH_SHORT).show();
                }

            } else {
                Log.i("XMPPChatDemoActivity", "no massage send");
                Log.e("XMPPChatDemoActivity", "Failed to log in as "
                        + connection.getUser());
            }

            break;
        }
    }

call setConnection(connection); after getting user login

For receiving message:

public void setConnection(XMPPConnection connection) {
        this.connection = connection;
        if (connection != null) {
            // Add a packet listener to get messages sent to us
            PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
            connection.addPacketListener(new PacketListener() {
                @Override
                public void processPacket(Packet packet) {
                    Message message = (Message) packet;
                    if (message.getBody() != null) {
                        String fromName = StringUtils.parseBareAddress(message
                                .getFrom());
                        Log.i("XMPPChatDemoActivity", "Text Recieved "
                                + message.getBody() + " from " + fromName);
                        messages.add(fromName + ":  " + message.getBody());
                        //messages.add(fromName + ":");
                        //messages.add(message.getBody());
                        // Add the incoming message to the list view
                        mHandler.post(new Runnable() {
                            public void run() {
                                setListAdapter();
                            }
                        });
                    }
                }
            }, filter);
        }
    }


标签: android xmpp