【问题标题】:receiving message in xmmp在 xmpp 中接收消息
【发布时间】:2012-10-09 13:45:07
【问题描述】:

如何接收来自服务器的消息?我有一部分可以向服务器发送消息但不能从服务器接收消息?它应该怎么做?我正在使用 xmpp 作为传输协议

package com.example.xmpp.asmack;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

    public class XMPPAsATransportProtocol extends Activity implements OnClickListener {
        XMPPConnection conn;
        TextView output;
        final String HOST = "vps01.fit3140hack.org";
        final int PORT = 5222;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.d("xmppasatransportprotocol", "got to start");
            setContentView(R.layout.main);
            View createbutton = findViewById(R.id.button1);
            View messagebutton = findViewById(R.id.button2);
            View closebutton = findViewById(R.id.button3);
            output = (TextView) findViewById(R.id.textView2);
            Log.d("xmppasatransportprotocol", "got buttons");

            createbutton.setOnClickListener(this);
            messagebutton.setOnClickListener(this);
            closebutton.setOnClickListener(this);
        }

        private void sendMessage() throws XMPPException {
            Log.d("xmppasatransportprotocol", "sendmessage");
            Chat chat = conn.getChatManager().createChat("22686274test2@" + HOST, null);
            EditText message = (EditText)findViewById(R.id.editText1);
            chat.sendMessage(message.getText().toString());
        }
        private void openConnection() throws XMPPException {
            Log.i("xmppasatransportprotocol", "openconnection");
            ConnectionConfiguration config = new ConnectionConfiguration(HOST, PORT);
            config.setSASLAuthenticationEnabled(true);
            conn = new XMPPConnection(config);

            conn.connect();
            conn.login("22686274test1", "dummy");
        }
        private void closeConnection() {
            Log.i("xmppasatransportprotocol", "closeconnection");

            conn.disconnect();
        }

        public void onClick(View v) {
            try {
                switch(v.getId()) {
                    case R.id.button1:
                        openConnection();
                        Log.i("xmppasatransportprotocol", "Connected to " + HOST + ":" + PORT);
                        output.setText("Connected to " + HOST + ":" + PORT);
                        break;
                    case R.id.button2:
                        sendMessage();
                        Log.i("xmppasatransportprotocol", "message sent");
                        EditText message = (EditText)findViewById(R.id.editText1);
                        output.setText("Message sent:\n" + message.getText().toString());
                        break;
                    case R.id.button3:
                        closeConnection();
                        output.setText("Disconnected");
                        break;
                    default:    
                        throw new Exception("screwed up exception");
                }
            } catch (Exception e) {
                Log.d("xmppasatransportprotocol", "exception caught:" + e.getMessage());
                output.setText("Error: " + e.getMessage());
            }
        }
    }

【问题讨论】:

  • 文档非常明确。你试过什么?

标签: java android xmpp


【解决方案1】:
  1. 确保您在服务器中有您使用的帐户。

  2. 您需要监听错误。将侦听器附加到相应的连接发布者,如 conn.addConnectionCreationListener 等。为此,您需要实现 MessageListener、ConnectionCreationListener、org.jivesoftware.smack.ConnectionListener、PacketListener 等接口。在实现这些接口时,覆盖 connectionClosedOnError(Exception e) 可能会让您知道出了什么问题,在覆盖的方法 processPacket(Packet packet) 中,您应该检查如下错误:

    if (packet.getError() != null) 
    {
        Exception e = new XMPPException(packet.getError())
        System.out.println(e);
    }
    

【讨论】:

    猜你喜欢
    • 2011-12-17
    • 2013-06-28
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多