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

时间:2013-06-07 10:52:21

标签: android xmpp chat smack asmack

我正在使用XMPP协议在Android中编写聊天客户端。我使用了asmack.jar http://asmack.freakempire.de/。该实现在我测试的普通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>

1 个答案:

答案 0 :(得分:0)

您必须为MessageListener实施MainActivity课程才能接收聊天消息