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

时间:2015-01-19 11:35:30

标签: android xmpp

我是聊天应用的初学者。 我搜索并研究了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);
    }

我正在使用此代码,不知道为什么消息不是通过另一方发送的 任何人都可以告诉我在解决这个问题时犯的错误或帮助

1 个答案:

答案 0 :(得分:1)

我找到了这个解决方案:

用于发送消息

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);获得用户登录后

用于接收消息:

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