Can not receive chat messages in Android using XMPP and aSmack
I am writing a chat client in Android using the XMPP protocol. I have used the asmack.jar as provided by http://asmack.freakempire.de/. The implementation works in plain Java (using smack.jar) which I have tested. But in Android, I can only send messages to the other client (he uses pidgin) and cannot receive messages from him. The app successfully connects to the server, logs in and appears online but simply doesn't receive any message.
My processMessage()
never gets called nor does chatCreated()
when a message is send to my client.
My Activity class:
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
}
}
}
Manifest file:
<?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
类才能接收聊天消息