My Question ->
I am trying to send a custom iq to ejabberd server (I was successful in that) and then perform some custom operations on the server (also done) and then send a response from server to android client with custom parameters/attributes(Also done, but after getting this result in android is where I am stuck). Example:
I am sending this iq
SENT (1): <iq to='example.com' id='D2IDi-16' type='get'><query xmlns='ns:custom'><a/></query></iq>
But I wanted this response in some variable or something on Android side for further computation but somehow I haven’t been able to achieve it.
I have searched for it a really long time now and I have found most of the example outdated, or should i say smack have grown from then.
To be specific my code gives compilation error at line
saying that "There is no default constructor available in ‘org.jivesoftware.smack.packet.IQ’ " where as in all the examples I have seen this line runs fine.
So what am I missing ? If this is wrong approach then how can I correct it?
Please help me out with some examples.
Thank you.
A look into IQ.java reveals, that there is indeed no default constructor available. Here you have to call one of the three available constructors of the super class, eg super(childElementName, childElementNamespace);.
Edit: Some little nits:
I would name the IQCustom class IQCustomAuthProvider.
Also I would change this line to public class IQCustom extends IQProvider<IQCustomAuth> {, so that its clear that the Provider returns a IQCustomAuth object, not an IQ.
@Hiren1 sorry but I am not able to completely understand your point. Can you please give a example?
What I did after reading your point was created a object of class IQCustomAuth like this in the Main Activity
final IQCustomAuth iqcustomauth = new IQCustomAuth("testing7@example.com", "example.com");
try {
connection.sendIqWithResponseCallback(iqcustomauth, new PacketListener() {
@Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
IQCustomAuth IQ = (IQCustomAuth)packet;
System.out.println("################# Custom packet received"+IQ.getServerTime_Local() +" "+IQ.getServerTime_UTC());
}
});
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
But with no success
05-23 18:06:07.909 26557-26780/com.example.admin.erl_chat E/AbstractXMPPConnection: Exception in async packet listener
java.lang.ClassCastException: org.jivesoftware.smack.packet.UnparsedIQ cannot be cast to com.example.admin.erl_chat.IQCustomAuth
at com.example.admin.erl_chat.MainActivity$1.processPacket(MainActivity.java:114)
at org.jivesoftware.smack.AbstractXMPPConnection$5.processPacket(AbstractXMPPConnection.java:1424)
at org.jivesoftware.smack.AbstractXMPPConnection$3.run(AbstractXMPPConnection.java:1101)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
where MainActivity.java:114 is
IQCustomAuth IQ = (IQCustomAuth)packet;
What did I do wrong?
ProviderManager.addExtensionProvider("response", "ns:custom", new IQCustom());
In the gist It was done inside the on click listener of the button but I shifted it to the onCreate() method.
And the above exception occurred after I had done all this already.
??
Thank you Hiren for your kind response. But Actually I understood your suggestion and made a class with getters and setters and got it working.So a big thank you