I've been having trouble sending/receiving messages with Smack and Openfire. Currently my setup involves two computers. Both computers are running an android emulator that launches the program. The program first launches a login activity, then launches a chat interface activity. The chat interface currently does not do anything; once the ChatInterface activity launches, a message is automatically sent.
One computer (laptop) acts like a server, and the desktop computer connects to the laptop server (Openfire). Looking at the Openfire user summary on the laptop, both users seem to be available and ready to chat. However, when its time to receive messages, nothing appears. The messages are not printed on a view object such as ListView, but on a log.
The following is the laptop code:
private static final String TAG = "MSG";
private ListView screen;
private EditText textEditor;
private Button sendButton;
private final String Host = "10.0.2.2";
private final int port = 5222;
private AbstractXMPPConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_interface);
screen = (ListView) findViewById(R.id.ScreenView);
textEditor = (EditText) findViewById(R.id.TextEditBox);
sendButton = (Button) findViewById(R.id.SendButton);
Log.d(TAG, "onCreate Chat Interface");
final String username = getIntent().getExtras().getString("Username");
final String password = getIntent().getExtras().getString("Password");
new AsyncConnect().execute(username, password);
sendButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
}
});
Log.d(TAG, "Success");
}
public class AsyncConnect extends AsyncTask<String, Void, Void>
{
ProgressDialog pdLoading = new ProgressDialog(ChatInterface.this);
protected void onPreExecute()
{
pdLoading.setMessage("Loading...");
pdLoading.show();
}
protected Void doInBackground(String...params)
{
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword(params[0], params[1]);
configBuilder.setServiceName("Openfire");
configBuilder.setHost(Host);
configBuilder.setPort(port);
configBuilder.build();
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); // Remove this later could be security threat
connection = new XMPPTCPConnection(configBuilder.build());
try {
connection.connect();
connection.login();
Log.d(TAG, "Logged on");
Log.d(TAG, "Connection: " + connection.getUser());
if(params[0].equals("test2") && params[1].equals("test2!")) {
Log.d(TAG, "Entered test2");
ChatManager chatManager = ChatManager.getInstanceFor(connection);
Log.d(TAG, "ChatManager");
Chat newChat = chatManager.createChat("LoveJack@yahoo.com", new ChatMessageListener(){
public void processMessage(Chat chat, Message message)
{
Log.d(TAG, "Received Message From Desktop: " + message.getBody());
}
});
newChat.sendMessage("This is Jane Doe");
chatManager.addChatListener(
new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean createdLocally) {
Log.d(TAG, "Entered chatCreated");
if (!createdLocally) {
Log.d(TAG, "Entered Locally");
chat.addMessageListener(new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
try {
Log.d(TAG, "Incoming message...");
Log.d(TAG, "Received message from Desktop: " + message.getBody());
Log.d(TAG, "Message Received");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
});
Log.d(TAG, "Listened");
}
}
catch(SmackException.ConnectionException e)
{
e.printStackTrace();
Log.d(TAG, "SmackException.ConnectionException");
}
catch(XMPPException e)
{
e.printStackTrace();
Log.d(TAG, "XMPPException");
}
catch(IOException e)
{
e.printStackTrace();
Log.d(TAG, "IOException");
}
catch(SmackException e)
{
e.printStackTrace();
Log.d(TAG, "SmackException");
}
return null;
}
protected void onPostExecute(Void params)
{
pdLoading.dismiss();
}
}
The following is the desktop code:
private static final String TAG = "MSG";
private ListView screen;
private EditText textEditor;
private Button sendButton;
private final String Host = "192.168.1.152";
private final int port = 5222;
private AbstractXMPPConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_interface);
screen = (ListView) findViewById(R.id.ScreenView);
textEditor = (EditText) findViewById(R.id.TextEditBox);
sendButton = (Button) findViewById(R.id.SendButton);
Log.d(TAG, "onCreate Chat Interface");
final String username = getIntent().getExtras().getString("Username");
final String password = getIntent().getExtras().getString("Password");
sendButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
}
});
new AsyncConnect().execute(username, password);
Log.d(TAG, "Success");
}
public class AsyncConnect extends AsyncTask<String, Void, Void> {
ProgressDialog pdLoading = new ProgressDialog(ChatInterface.this);
protected void onPreExecute() {
pdLoading.setMessage("Loading...");
pdLoading.show();
}
protected Void doInBackground(String...params) {
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword(params[0], params[1]);
configBuilder.setServiceName("Openfire");
configBuilder.setHost(Host);
configBuilder.setPort(port);
configBuilder.build();
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); // Remove this later could be security threat
connection = new XMPPTCPConnection(configBuilder.build());
try {
connection.connect();
connection.login();
Log.d(TAG, "Logged on");
Log.d(TAG, "Connection: " + connection.getUser());
if(params[0].equals("test1") && params[1].equals("test1!"))
{
Log.d(TAG, "Entered test1");
ChatManager chatmanager = ChatManager.getInstanceFor(connection);
Log.d(TAG, "ChatManager");
Chat newChat = chatmanager.createChat("HumbleBee001@yahoo.com", new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
Log.d(TAG, message.getBody());
}
});
newChat.sendMessage("This is John Doe");
Log.d(TAG, newChat.getParticipant());
Log.d(TAG, "Message sent");
}
}
catch(SmackException.ConnectionException e)
{
e.printStackTrace();
Log.d(TAG, "SmackException.ConnectionException");
}
catch(XMPPException e)
{
e.printStackTrace();
Log.d(TAG, "XMPPException");
}
catch(IOException e)
{
e.printStackTrace();
Log.d(TAG, "IOException");
}
catch(SmackException e)
{
e.printStackTrace();
Log.d(TAG, "SmackException");
}
return null;
}
protected void onPostExecute(Void params)
{
pdLoading.dismiss();
}
}
Neither processMessages methods are being called as nothing is printed in the logcat.
I'm not quite sure what the problem is. Some of the Smack documentation seems to be outdated, and there not a lot of great resources for the API. Does anybody have a clue on what could be the problem?
I see many problems, I suggest to read better the documentations.
What I found out:
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword(params[0], params[1]);
configBuilder.setServiceName("Openfire");
configBuilder.setHost(Host);
configBuilder.setPort(port);
configBuilder.build();
ServiceName
it's the name of your server, by default it's machine-name where it's installed Openfire. You will have troubles if you didn't a specific configuration to assign this name. Check the correct name on admin control panel (Openfire log will says where it listens... something like yourmachinename:9090
)
It's much better to split connection functionaly from login functionality.
I suggest to remove the setUsernameAndPassword and use the params[0]
and params[1]
in connection.login()
call.
You miss the Resource (it's a String like "Spark", "Smack", "Android", "MyXmppClient") and there are some default configurations and issues if you miss it.
so I suggest to change, for example, with this:
connection.login(param[0],param[1],"MyAndroidApp");
username must be lowercase, Openfire has some defensive toLowerCase() in his code but it's better to avoid uppercase letters. So be sure to use JID in lowercase.
LoveJack@yahoo.com has to be: lovejack@yahoo.com
Howerver, it's not about an email: you need a JID, so it's not "yahoo.com" but @yourservername
(same issue of ServiceName I talked about before).
You are trying to create a chat with "LoveJack" on XmppServer named "yahoo.com", I don't think this is your target.
Probably you didn't register correctly users you are using on server, try to check (if talk on Yahoo it's what you want, maybe you have to read something like this)