gpt4 book ai didi

android - 如何在 ASMACK 中解析 CustomIQ

转载 作者:行者123 更新时间:2023-11-29 00:21:41 25 4
gpt4 key购买 nike

我正在为我的应用程序使用 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,我在设置 XmppConnection 配置时添加了 IQProvider,使用 ProviderManager 类作为

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

最佳答案

也许已经有供应商注册了这个命名空间?也许您需要在 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();
}

}

关于android - 如何在 ASMACK 中解析 CustomIQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22566411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com