Not able to send messages using smack 4.1.3 from android emultor

HI Guys

Please help me. I am developing android xmpp Client using Smack 4.1.3 + Openfire 3.10.2 server. I am able to receive the messages sent from SPARK windows client but not able send the message to the same. Kindly help me… here is my code :

public void sendMessage(String buddyJID, String messageTe) {

String OurBuddy = buddyJID + “@” + ChatContants.SERVICENAME;

String from = connection.getUser();

Log.d(TAG,

String.format(

“Connection is null in sendMessage Sending mesage ‘%1$s’ to user %2$s from Me %3$s”,

messageTe, OurBuddy, from));

if (chatManager != null) {

Message msgObj = new Message();

msgObj.setBody(messageTe);

msgObj.setTo(OurBuddy);

msgObj.setFrom(from);

msgObj.setType(Message.Type.chat);

msgObj.addExtension(new DefaultExtensionElement(“from”, from));

msgObj.addExtension(new DefaultExtensionElement(“to”, OurBuddy));

try {

connection.sendStanza(msgObj);

///LOgs sent message

} catch (NotConnectedException e) {

// TODO Auto-generated catch block

Log.d(TAG, “sendStanza is creating some problem”);

e.printStackTrace();

}

} else {

Log.d(TAG, “XMPPMANGER sendMEssage chamAnaeger is Null”);

}

}

ChatContants.SERVICENAME is same as provided in openfire domain name,

Already connected and logged in.

I am getting the sent message logs but not receiving any thing in SPARK client.

can you give me you code ,i havn’t mean

Please find the attached code.

put all libraries of smack 4.1.3 and use your server and domain name in ChatContants class…

Please help me …

Also provide some suggestion for better functioning of this chat app. It is in its beginning phase of development.

Thanx in advance.
TestingChat.zip (152774 Bytes)

connection.sendStanza(msgObj);

this code doesn’t send a message

//get a chatmanager

ChatManager cm = ChatManager.getInstanceFor(connection);

//add listener

// if receive a message ,this will get

cm.addChatListener(new ChatManagerListener(){

@Override

public void chatCreated(Chat arg0, boolean arg1) {

arg0.addMessageListener(new ChatMessageListener(){

@Override

public void processMessage(Chat arg0, Message arg1) {

if(null!=arg1.getBody())

{

String from = arg1.getFrom().substring(0,arg1.getFrom().indexOf("@"));

System.out.println("from “+from+” : "+arg1.getBody());

}

}

});

}

});

//this send a mesage to someone

Chat chat = cm.createChat("zxc@192.168.1.189");

chat.sendMessage(“abcdefg”);

i use this function ,can send a message to someone

which version jdk??

Use samck4.1.4 android as above must use jdk7 version?

Thnx for reply @zhengxiujiang

I am using java version “1.8.0_31” ,

I also try to send message as you have suggested.

but I have used my openfire server domain name instead of IP Address in JID.(I think it should be fine.)

Chat chat = cm.createChat(“zxc@domain-pc”);

chat.sendMessage(“abcdefg”);

still not able to send messages to the server while I am perfectly receiving the messages.

i think of it

Chat chat = cm.createChat(“zxc@domain-pc”);

This “domain-pc” is your service name.

Connection.setServiceName("")

It is this properties

not you host.

Hey @zhengxiujiang

I got the problem …

I added a code line xmmpManager.getConnection().getUser() and it return **NULL **(which indicates that I am logged out of server) But openfire server shows that I am still logged in .

Thats why I was unable to send Message.

Now for a workaround, I added login code before each message send code… But it is returning me AlreadyLoggedInException in try catch.

and message is successfully sent to the client.

Please help me… How can I avoid such scenerios.

Hello,

This is my code for send messages for individual chat:

public boolean sendmessage(String body, String destinationuser){

String from = mConnection.getUser();

ChatManager chatManager = ChatManager.getInstanceFor(mConnection);

Message message = new Message(destinationuser, Message.Type.chat);

message.setFrom(from);

message.setBody(body);

message.addExtension(new DefaultExtensionElement(“from”,from));

message.addExtension(new DefaultExtensionElement(“to”, destinationuser));

try {

mConnection.sendStanza(message);

} catch (SmackException.NotConnectedException e) {

e.printStackTrace();

return false;

}

return true;

}

You don’t need add login code before each message send code. Only when the user is offline.