在 android 上通过 xmpp 发送和接收消息

sending and receiving message through xmpp on android

我是聊天应用的新手。 我搜索并研究了 xmpp 服务器。 我安装了 openfire xmpp 服务器。 完成与 xmpp 服务器的连接、注册和登录任务。 现在我正在尝试发送和接收消息。 用户登录后,然后用户输入要发送的文本。我使用此代码发送消息

    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;
        }
    }

以及我用来接收消息的代码,请查看此 screenshot 以查看用户,

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);
    }

我正在使用此代码不知道为什么消息没有发送到另一端 任何人都可以告诉我犯了什么错误或帮助解决这个问题

我找到了这个解决方案:

用于发送消息

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;
        }
    }

调用 setConnection(连接);获得用户登录后

接收消息:

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);
        }
    }