Reputation: 3645
I'm using asmack the latest version (asmack-android-8-source-0.8.3) in a android project and I'm trying to communicate with the server by sending multiple IQs and receiving responses. I receive the response and it's parsed correctly but the package listener it's not triggering. I have the following code:
vCard_IQProvider.java
public class vCard_IQProvider implements IQProvider
{
public static final String NAMESPACE = "vcard-temp";
public static final String ELEMENT_NAME = "vCard";
public static final String LAST_STATUS = "LAST_STATUS";
public static final String LAST_STATUS_DESCRIPTION = "LAST_STATUS_DESCRIPTION";
public static final String WORK_TIME_RANGE = "WORK_TIME_RANGE";
public static final String SOUND_SETTINGS = "SOUND_SETTINGS";
public static final String AUTO_LOCATION_MACHINE_DATA = "AUTO_LOCATION_MACHINE_DATA";
public static final String AUTO_LOCATION = "AUTO_LOCATION";
public static final String AUTO_LOCATION_ENABLED = "AUTO_LOCATION_ENABLED";
public static final String AUTO_ONLINE = "AUTO_ONLINE";
public static final String HIDEACTIONNOTIFICATIONS = "HideActionNotifications";
public static final String AUTO_CONNECT = "AUTO_CONNECT";
public static final String AUTO_OFFLINE_WORK_TIME = "AUTO_OFFLINE_WORK_TIME";
public static final String AUTO_RELOGIN = "AUTO_RELOGIN";
public static final String CONNECTED_VIA_INTERNET = "CONNECTED_VIA_INTERNET";
public static final String MINIMIZE_CHAT = "MINIMIZE_CHAT";
public static final String PROMPT_PROJECT_SWITCH = "PROMPT_PROJECT_SWITCH";
public static final String MACHINE_NAME = "MACHINE_NAME";
public static final String MROFFICE_VER = "MROFFICE_VER";
public static final String WORK = "WORK";
public static final String LOCALITY = "LOCALITY";
public static final String TIMESTAMP = "timestamp";
public static final String REGION = "REGION";
public static final String EXT = "EXT";
public static final String LAST_ACTIVITY_TS = "LAST_ACTIVITY_TS";
public static final String FUTURE_STATUS = "FUTURE_STATUS";
public static final String FUTURE_STATUS_DESCRIPTION = "FUTURE_STATUS_DESCRIPTION";
public static final String FUTURE_STATUS_TS = "FUTURE_STATUS_TS";
public static final String CUSTOM = "CUSTOM";
public static final String PREF = "PREF";
private Map<String, String> list = new HashMap<String, String>();
@Override
public IQ parseIQ(XmlPullParser parser) throws Exception
{
String name;
boolean isEmpty;
boolean done = false;
while(parser.next() != XmlPullParser.END_DOCUMENT && false == done)
{
name = parser.getName();
switch (parser.getEventType())
{
case XmlPullParser.START_TAG:
{
isEmpty = parser.isEmptyElementTag();
if(name.equalsIgnoreCase(LAST_STATUS) && false == isEmpty)
{
list.put(LAST_STATUS, parser.nextText());
}
else if(name.equalsIgnoreCase(LAST_STATUS_DESCRIPTION) && false == isEmpty)
{
list.put(LAST_STATUS_DESCRIPTION , parser.nextText());
}
else if(name.equalsIgnoreCase(WORK_TIME_RANGE) && false == isEmpty)
{
list.put(WORK_TIME_RANGE, parser.nextText());
}
else if(name.equalsIgnoreCase(SOUND_SETTINGS) && false == isEmpty)
{
list.put(SOUND_SETTINGS, parser.nextText());
}
else if(name.equalsIgnoreCase(AUTO_LOCATION_MACHINE_DATA) && false == isEmpty)
{
list.put(AUTO_LOCATION_MACHINE_DATA, parser.nextText());
}
else if(name.equalsIgnoreCase(AUTO_LOCATION) && false == isEmpty)
{
list.put(AUTO_LOCATION, parser.nextText());
}
else if(name.equalsIgnoreCase(AUTO_LOCATION_ENABLED) && false == isEmpty)
{
list.put(AUTO_LOCATION_ENABLED, parser.nextText());
}
else if(name.equalsIgnoreCase(AUTO_ONLINE) && false == isEmpty)
{
list.put(AUTO_ONLINE, parser.nextText());
}
else if(name.equalsIgnoreCase(HIDEACTIONNOTIFICATIONS) && false == isEmpty)
{
list.put(HIDEACTIONNOTIFICATIONS, parser.nextText());
}
else if(name.equalsIgnoreCase(AUTO_CONNECT) && false == isEmpty)
{
list.put(AUTO_CONNECT, parser.nextText());
}
else if(name.equalsIgnoreCase(AUTO_OFFLINE_WORK_TIME) && false == isEmpty)
{
list.put(AUTO_OFFLINE_WORK_TIME, parser.nextText());
}
else if(name.equalsIgnoreCase(AUTO_RELOGIN) && false == isEmpty)
{
list.put(AUTO_RELOGIN, parser.nextText());
}
else if(name.equalsIgnoreCase(CONNECTED_VIA_INTERNET) && false == isEmpty)
{
list.put(CONNECTED_VIA_INTERNET, parser.nextText());
}
else if(name.equalsIgnoreCase(MINIMIZE_CHAT) && false == isEmpty)
{
list.put(MINIMIZE_CHAT, parser.nextText());
}
else if(name.equalsIgnoreCase(PROMPT_PROJECT_SWITCH) && false == isEmpty)
{
list.put(PROMPT_PROJECT_SWITCH, parser.nextText());
}
else if(name.equalsIgnoreCase(MACHINE_NAME) && false == isEmpty)
{
list.put(MACHINE_NAME, parser.nextText());
}
else if(name.equalsIgnoreCase(MROFFICE_VER) && false == isEmpty)
{
list.put(MROFFICE_VER, parser.nextText());
}
else if(name.equalsIgnoreCase(WORK) && false == isEmpty)
{
list.put(WORK, parser.nextText());
}
else if(name.equalsIgnoreCase(LOCALITY) && false == isEmpty)
{
list.put(LOCALITY, parser.nextText());
}
else if(name.equalsIgnoreCase(TIMESTAMP) && false == isEmpty)
{
list.put(TIMESTAMP, parser.nextText());
}
else if(name.equalsIgnoreCase(REGION) && false == isEmpty)
{
list.put(REGION, parser.nextText());
}
else if(name.equalsIgnoreCase(EXT) && false == isEmpty)
{
list.put(EXT, parser.nextText());
}
else if(name.equalsIgnoreCase(LAST_ACTIVITY_TS) && false == isEmpty)
{
list.put(LAST_ACTIVITY_TS, parser.nextText());
}
else if(name.equalsIgnoreCase(FUTURE_STATUS) && false == isEmpty)
{
list.put(FUTURE_STATUS, parser.nextText());
}
else if(name.equalsIgnoreCase(FUTURE_STATUS_DESCRIPTION) && false == isEmpty)
{
list.put(FUTURE_STATUS_DESCRIPTION, parser.nextText());
}
else if(name.equalsIgnoreCase(FUTURE_STATUS_TS) && false == isEmpty)
{
list.put(FUTURE_STATUS_TS, parser.nextText());
}
else if(name.equalsIgnoreCase(CUSTOM) && false == isEmpty)
{
list.put(CUSTOM, parser.nextText());
}
else if(name.equalsIgnoreCase(PREF) && false == isEmpty)
{
list.put(PREF, parser.nextText());
}
break;
}
case XmlPullParser.END_TAG:
{
done = ELEMENT_NAME.equalsIgnoreCase(name);
break;
}
}
}
name = null;
return new vCard_IQ(list);
}
}
vCard_IQ.java
public class vCard_IQ extends IQ
{
public static final String ID = "vcard";
private static Map<String, String> list;
private static boolean finishedParsing = false;
public vCard_IQ(Map<String, String> l)
{
if(null == list)
{
list = new HashMap<String, String>();
}
list.clear();
list.putAll(l);
finishedParsing = true;
}
@Override
public String getChildElementXML()
{
return null;
}
public static final Map<String, String> getData()
{
return list;
}
public static void setFinishedParsingToFalse()
{
finishedParsing = false;
}
public static final boolean finishedParsing()
{
return finishedParsing;
}
}
I add the provider:
ProviderManager.getInstance().addIQProvider(vCard_IQProvider.ELEMENT_NAME, vCard_IQProvider.NAMESPACE, new vCard_IQProvider());
and the package listener:
connection.addPacketListener(new PacketListener()
{
@Override
public void processPacket(Packet p)
{
if(p.getPacketID().equals(vCard_IQ.ID))
{
vCard_IQ pp = (vCard_IQ)p;
//access the parsed data
//vCard_IQ.getData().get.......
pp = null;
}
}
},
new PacketFilter()
{
@Override
public boolean accept(Packet arg0)
{
return true;
}
});
The packet filter is set to accept all packets, the listner is not triggering for some reason. I can see in the debugger that the server is sending the responses.
I even tried to bypass the listener by creating an asynk task an waiting in the background thread until the response is parsed and than I access it. Now it works only for the first iq sent - I receive a response and it's parsed correctly, but for the rest I can see in the debugger that the server it's sending responses but it never reaches the parser. The parser it's never called.
Asynk<Void, Void, Void> asynk = new Asynk<Void, Void, Void>()
{
Packet iq_vcard;
@Override
protected Void doInBackground(Void... params)
{
for(String s : names_list)
{
final String name = s;
iq_vcard = new Packet()
{
@Override
public String toXML()
{
String str = String.format("<iq from='%s' to='%s' type='get' id='" + vCard_IQ.ID + "'><vCard xmlns='vcard-temp'/></iq>",
sharedPrefs.getString(LogIn.USERNAME, "") + "@" + sharedPrefs.getString(Settings_LogIn.DOMAIN, Settings_LogIn.ERROR) + "/iOffice",
name + "@" + sharedPrefs.getString(Settings_LogIn.DOMAIN, Settings_LogIn.ERROR));
Log.e("iq_vcard", str);
return str;
}
};
connection().sendPacket(iq_vcard);
iq_vcard = null;
while(false == vCard_IQ.finishedParsing())
{
try
{
Thread.sleep(1000);
Log.e("TAG", "waiting to finish parsing...");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
//access the parsed data
//vCard_IQ.getData().get.......
vCard_IQ.setFinishedParsingToFalse();
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
}
};
asynk.execute();
Any suggestions on what's wrong?
Upvotes: 3
Views: 1333
Reputation: 1
Just if somebody is trying to do the same, I found what was the problem.
The while condition on the vCard_IQProvider
class is wrong, it should be:
while(!done && parser.next() != XmlPullParser.END_DOCUMENT)
Otherwise when done is set to true, the while will check again the condition and move the parser to the next element (with the call parser.next()
).
Upvotes: 0
Reputation: 612
From documentation: http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smack/provider/IQProvider.html
At the end of the method call, the parser must be positioned on the closing tag of the child element.
Upvotes: 0
Reputation: 3645
This is a bug in aSmack and after numerous attempts at fixing this issue, failed attempts of compiling aSmack myself following this tutorial and trying anything that I could think of, I did the following: I created a new package in my android project and dumped all of the aSmack's java classes there. This way I let eclipse compile aSmack and I got a better control over it's code.
The problem was that every time I would receive an custom iq response from server, in PacketParserUtils.java aSmack would return a wrong elementName and namespace, thus choosing a wrong iq provider to parse the response.
Upvotes: 1