使用XMPP和aSmack无法在Android中接收聊天消息

我正在使用XMPP协议在Android中撰写聊天客户端。 我使用了http://asmack.freakempire.de/提供的asmack.jar。 这个实现在我测试过的普通Java(使用smack.jar)中工作。 但在Android中,我只能将消息发送给其他客户端(他使用pidgin),并且无法接收来自他的消息。 该应用程序成功连接到服务器,登录并显示在线,但根本没有收到任何消息。

当消息发送给我的客户端时,我的processMessage()永远不会被调用,也不会执行chatCreated()

我的活动课程:

package com.example.basicchat;

import org.jivesoftware.smack.AndroidConnectionConfiguration;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.SmackAndroid;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SmackAndroid.init(this);
        MainActivity2 xmppClient= new MainActivity2("jabber.org", 5222);
        try {
            xmppClient.login();
        } catch (XMPPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

        public class MainActivity2 implements MessageListener, ChatManagerListener, ConnectionListener {


        private String    server;
        private int        port;
        public MainActivity2( String server, int port )
    {
        super();

        this.server = server;
        this.port = port;
    }
        private XMPPConnection    connection    = null;

        public void login() throws XMPPException
        {
            String username = "my_user";
            String password = "xxxxxxxx";
            login(username, password);

        }

        private void login(String userName, String password) throws XMPPException
        {
           AndroidConnectionConfiguration config = new AndroidConnectionConfiguration(server,
                    port);
            connection = new XMPPConnection(config);
            connection.connect();
            connection.addConnectionListener(this);
            connection.login(userName, password);
            ChatManager chatManager = connection.getChatManager();
            chatManager.addChatListener(this);
            Toast.makeText(MainActivity.this,"listener set", Toast.LENGTH_SHORT).show();
           // sendMessage("helloooo","command_server@jabber.org"); /* this mssg is sent */
        }

        private void sendMessage(String message, String to) throws XMPPException
        {
            Chat chat = connection.getChatManager().createChat(to, this);
            chat.sendMessage(message);
        }

        public void disconnect()
        {
            connection.disconnect();
        }

        /** never gets called **/
        @Override
        public void processMessage(Chat chat, Message message)
        {
            /*********** Handle Request and construct response ******************/
            Toast.makeText(MainActivity.this,"mssg: "+message.getBody(), Toast.LENGTH_SHORT).show();
            switch (message.getType())
            {
            case chat:
                String jsonData = (null==message.getBody())?"":message.getBody();

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

                break;
            }
        }
        @Override
        public void chatCreated(Chat arg0, boolean arg1) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this,"Chat Created!", Toast.LENGTH_SHORT).show();
            if (!arg1)
                arg0.addMessageListener(this);

        }
        @Override
        public void connectionClosed() {
            // TODO Auto-generated method stub

        }
        @Override
        public void connectionClosedOnError(Exception arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void reconnectingIn(int arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void reconnectionFailed(Exception arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void reconnectionSuccessful() {
            // TODO Auto-generated method stub

   }

    }

}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.basicchat"
    android:versionCode="1"
    android:versionName="1.0" >

 <uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
         >

        <activity
            android:name="com.example.basicchat.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>

</manifest>

您必须实现MainActivity MessageListener类才能接收聊天消息

链接地址: http://www.djcxy.com/p/64987.html

上一篇: Can not receive chat messages in Android using XMPP and aSmack

下一篇: How to Create a XMPP Chat Client in Android with Openfire?