我正在为我的应用程序使用ASMACK库。我从服务器收到了以下IQ。
<iq id='bind_2' type='result'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid username='xx' fullname='yy'>xx@aa.bb.cc.dd/Resource</jid></bind></iq>
为解析此IQ,我在使用 ProviderManager 类设置 XmppConnection 配置时添加了IQProvider
ProviderManager.getInstance().addIQProvider("bind",
"urn:ietf:params:xml:ns:xmpp-bind",
new CustomIQProvider());
我的 CustomIQProvider 类是
public class CustomIQProvider implements IQProvider {
public static final String NAME_SPACE = "urn:ietf:params:xml:ns:xmpp-bind";
@Override
public IQ parseIQ(XmlPullParser parser) throws Exception {
Log.i("My_TAG", "inside LivBindIQ");
CustomIQ bindIQ = new CustomIQ();
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException(
"Parser not in proper position, or bad XML.");
}
boolean done = false;
while (!done) {
int eventType = parser.next();
if ((eventType == XmlPullParser.START_TAG)
&& ("entry".equals(parser.getName()))) {
eventType = parser.next();
String name = parser.nextText();
eventType = parser.next();
String value = parser.nextText();
}
else if (eventType == XmlPullParser.END_TAG) {
done = true;
}
}
return bindIQ;
}
}
我的 CustomIQ 类是,
public class CustomIQ extends IQ {
@Override
public String getChildElementXML() {
// TODO Auto-generated method stub
return null;
}
}
现在我很困惑,如何执行CustomIQProvider
类..我没有从CustomIQProvider
类获得任何日志。我的步骤是否正确,否则我需要执行任何调用CustomIQProvider
类
答案 0 :(得分:2)
也许有一个提供商已经注册了这个命名空间? 也许您需要在IQProvider上对XML进行完整而正确的解析,并且您的CustomIQ需要实现xml的正确输出。 您正在检查标记,显然它不存在,因此该解析器将永远不会工作。 假设这是您要解析的标记
<jid username='xx' fullname='yy'>xx@aa.bb.cc.dd/Resource</jid>
我没有测试但是这应该有用,你的parseIQ方法应该是这样的
public IQ parseIQ(XmlPullParser parser) throws Exception {
CustomIQ bindIQ = new CustomIQ();
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("jid")) {
String username = parser.getAttributeValue("", "username");
String fullname = parser.getAttributeValue("", "fullname");
String jidValue = parser.nextText();
bindIQ.setUsername(username);
bindIQ.setFullname(fullname);
bindIQ.setFullJid(jidValue);
}
}
else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("jid")) {
done = true;
}
}
}
return bindIQ;
}
您的CustomIQ课程:
public class CustomIQ extends IQ {
private String username;
private String fullName;
private String fullJid;
public CustomIQ() {
}
public void setUsername(String username) {
this.username = username;
}
public void setFullname(String fullName) {
this.fullName = fullName;
}
public void setFullJid(String fullJid) {
this.fullJid = fullJid;
}
public String getUsername() {
return this.username;
}
public String getFullname() {
return this.fullName;
}
public String getFullJid() {
return this.fullJid;
}
@Override
public String getChildElementXML() {
StringBuilder builder = new StringBuilder("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"><jid username=\"");
builder.append(username).append("\"");
builder.append(" fullname=\"");
builder.append(fullName).append("\"");
builder.append(">");
builder.append(fullJid);
builder.append("</jid>");
builder.append("</bind>");
return builder.toString();
}
}