Sending and Receiving IQ Packets ASMACK/SMACK Android XMPP - android

I would highly appreciate if someone can help me how to recieve iq packets with ASMACK, i am sending raw iq packets but not able to receive it,
I have registered the iq packets programatically before making a connection but still not getting reponse,
pm.addIQProvider("vCard", "vcard-temp", new VCardProvider());
final IQ iq = new IQ() {
public String getChildElementXML() {
return "<iq from='test#XX.XX.XX.XX' id='v1' to='test#XX.XX.XX.XX' type='get'><vCard xmlns='vcard-temp'/></iq>";
}
};
iq.setType(IQ.Type.GET);
connection.sendPacket(iq);
connection.addPacketListener(new MyPacketListener(),new PacketTypeFilter(IQ.class));

First of all, try setting your packet listener before you send the packet. It is an asynchronous protocol and it is possible that the response is being returned before your listener is setup to receive it.
Then try setting -DsmackDebugEnabled=true to check that there is an actual response to your send.

You can implement a Packet Listner method, the processPacket(Packet packet) method will give you the incoming packets.
Here is a stack thread which explains the same issue aSmack - Packet to XML using Packet Listener outputs nullable elements

Related

How to read custom attributes of xmpp message tag in android?

The xml that i need to receive is :
<message id="qm5Dx8" to="adsfxcv" type="chat"from="adsf"
msgType="2"
thumbnail="randomThumbnail"
serverMediaURL="random"
isFromMe="1"
status="1"><body>Image</body><request xmlns='urn:xmpp:receipts'/></message>
Message is being sent by MyCustomMessage extends Message class.
In my message listener, Where i need to get the packet is :
public void processPacket(Packet packet) {
String recivedPacket = packet.toXML();
try {
if (packet instanceof MyCustomMessage) {
MyCustomMessage msg = (MyCustomMessage) packet;
....
But I am receiving only id,to,type and from in message tag. And the instance of packet is also of Message and it says , cannot cast packet to message. plz guide me how can i receive my desired packet.
You can't. Also you should never extend the Message or Presence class.
Never add custom values to specified stream element attributes (e.g. a new value for the type attribute of messages), and never add new attributes to top level elements (like you did with msgType, msgTimeStamp and so on).
This has the potential to break things! Don't do it. See also "XEP-0134: XMPP Design Guidelines § 2.1 XMPP is Sacred". That's why it's not possible in Smack. Instead, use a custom extension element, like xnyhps showed in his example (the data element). See also "RFC 6120 § 8.4 Extended Content" Those are called PacketExtension's in Smack.
See also this answer and question.

Unable to get Joined Chat Rooms using asmack

My problem is i am not been able to receive joined chat rooms. I am using the openfire server 3.8.2 and asmack library asmack-android-16.jar. I receive item-not-found error when i call getJoinedRooms function. though i can see the user is joined in the room from the admin console. Is it the server problem or the client problem or some issue with asmack? Please tell me if someone is able to get joined chat rooms using openfire and asmack for android.
here is how i am call the function:
Iterator RoomsIterator=MultiUserChat.getJoinedRooms(MyService.getConnection(),"user#192.168.1.3");
i also tried this but it gives no response form server:
Iterator RoomsIterator=MultiUserChat.getJoinedRooms(MyService.getConnection(),"user#192.168.1.3/Smack");
Please help me with my problem
Thanks in advance.
I solved my problem by adding a packet listener after call get joined rooms function.. as i was getting an empty list but when i debug i check that the rooms was getting returned in the resultant xml stanze that was sent by the server therefore i run the getjoinedroom function of asmack and then i manually add ha packet listener like this:
public void AddPacketListener(){
PacketFilter filter = new IQTypeFilter(IQ.Type.RESULT);
MyService.getConnection().addPacketListener(new PacketListener()
{
public void processPacket(Packet paramPacket) {
if(paramPacket.getFrom().equals(MyService.getConnection().getUser())){
String xml=paramPacket.toXML();
String from[];
System.out.println(xml);
from=paramPacket.getFrom().split("/");
Pattern pattern = Pattern.compile("<item jid=\"(.*?)/>");
Matcher matcher = pattern.matcher(xml);
String parts[];
Roomlist.clear();
while (matcher.find()) {
parts=matcher.group(1).split("#");
Roomlist.add(parts[0]);
}
return;
}
}
},filter);
}

Asmack android: Create a custom packet and register extension

I am using the library Asmack to use XMPP on an android app, the server sends custom Stanza
<notification xmlns="jabber:client" from="admin#api.pp.gs" to="1eef368606cb459b847809a0430bfa1f#api.pp.gs/iMac-de-Thomas" id="1457247499">
<body>lol</body>
</notification>
I would like to be able to listen these packets, I try to use PacketExtensionProvider but without success. Here my code to listen the packet:
xmppConnection.addPacketListener(new PacketListener()
{
#Override
public void processPacket(Packet packet)
{
Log.i("K:", "cool " + packet.getClass().toString());
}
}, new MyPaquetFilter());
Where MyPaquetFilter return always true for test purpose.
How should I use the PacketManager, PacketExtension stuff in order to get my custom paquet here?
In order to be able to receive a custom packet you need to first create a Provider, either an IQProvider or an PacketExtensionProvider, and register that Provider with ProviderManager, so that's considered for parsing when receiving a packet.
You didn't showed us your custom PacketExtensionProvider nor how you are registering it with the ProviderManager. And just to absolute precise, you need to wrap your custom extension notification within the message element if you use PacketExtensionProvider.

Using polling in jeromq

I am learning to use zeromq polling in android . I am polling on a req socket and a sub socket in the android program(client). So that this client can receive both reply messages from the server and also published messages.
My polling is not working. Both the req socket and the publish socket does not get polled in. If i don't use polling both the sockets receive the message.
I tried searching online but could not find anything relevant.
The client code is this :
public void run()
{
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket reqsocket = context.socket(ZMQ.REQ);
ZMQ.Socket subsocket =context.socket(ZMQ.SUB);
reqsocket.connect("tcp://10.186.3.174:8081");
subsocket.connect("tcp://10.186.3.174:8083");
subsocket.subscribe("".getBytes());
byte[] receivedmessage;
Poller poller=context.poller();
poller.register(reqsocket,Poller.POLLIN);
poller.register(subsocket,Poller.POLLIN);
reqsocket.send(msg.getBytes(),0);
while(!Thread.currentThread().isInterrupted())
{
if(poller.pollin(0))
{
receivedmessage=s.recv(0);
}
if(poller.pollin(0))
{
receivedmessage=subsocket.recv(0);
}
}
s.close();
context.term();
}
Am i missing out something or doing something wrong?
It looks like there are 3 problems with this.
The main one is you need to call poller.poll() as the first thing inside the while loop. This is why you are not getting any messages.
The next issue is that you're checking the same index for both sockets: I expect the second if statement needs to be
if(poller.pollin(1))
Lastly, the req socket requires a send before every receive, so the call to send needs to be inside the while loop, and before the poller.poll() you just added above :)

asmack pubsub getItems error

I'm trying to publish some information to server using the Pubsub nodes.unfortunately,I failed to retrieve the node that published before.Just very similar as the problem in the following link:
http://community.igniterealtime.org/message/199690#199690
to be specific, the code snippets like following :
try {
LeafNode node = mPubsub.getPEPNode(USEINFONODE,mFrom);
if(null != node){
List<Item> items = node.getItems();
Log.i("items",items.toString());
}
} catch (XMPPException e) {
Log.e("userInfoExtension","error : and the error is " + e.toString());
}
and the output error is no resposne from server.
the output of the debug is just like this:
<iq id="B9tI0-4" to="pubsub.mymachine" type="get"><query xmlns="http://jabber.org/protocol/disco#info" node="theNode"></query></iq>
<iq id="B9tI0-5" to="pubsub.mymachine" type="get"><pubsub xmlns="http://jabber.org/protocol/pubsub"><items node='theNode'/></pubsub></iq>
However according to the link mentioned above, the expected iq stanza shall be like this:
<iq type='get'
from='notifyserver#mymachine'
to='pubsub.mymachine'
id='items1'>
<query xmlns='http://jabber.org/protocol/disco#items'
node='theNode'/>
</iq>
so It shows that I miss the from field in the iq stanza,I'm wondering how can I put the from ='client#server' into the iq stanza. I have tried asmack libraries including :
asmack-android-7.jar , asmack-android-7-beem.jar asmack-android-16-beem.jar,all failed.
Can anyone help with this? Thanks very much.
I have found that it has something to do with receiving the packet. actually I have received the packet that I needed, the trouble is that the packet may can not be processed by smack in somewhere, and it will throw no response from sever exception.
so I think the problem is actually not receiving incoming packet correctly .
so does in my other question:
http://stackoverflow.com/questions/14357707/how-to-send-and-listen-to-custom-xmpp-presence-packet-with-asmack-the-library
I'm sorry to mislead you ! the error is caused by my extension provider which lead the parsing
packet into an endless loop, thus ,caused the no response from server exception.

Categories

Resources