How to parse a CustomIQ in ASMACK

2019-05-25 03:17发布

I am using ASMACK library fo my application. I received the following IQ from my server.

<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>

For parsing this IQ, I added the IQProvider while setting XmppConnection configuration by using ProviderManager class as

ProviderManager.getInstance().addIQProvider("bind",
                        "urn:ietf:params:xml:ns:xmpp-bind",
                        new CustomIQProvider());

And my CustomIQProvider class is

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;

    }

}

And my CustomIQ class is,

public class CustomIQ extends IQ {

    @Override
    public String getChildElementXML() {
        // TODO Auto-generated method stub
        return null;
    }

    }

Now I am confused about, how the CustomIQProvider class be executed.. I didn't get any log from CustomIQProvider class. Whether my steps are correct otherwise I need to do any steps for calling my CustomIQProvider class

1条回答
祖国的老花朵
2楼-- · 2019-05-25 03:26

Maybe there's a provider already register for this namespace? Perhaps you need to do the complete and correct parse of the XML on your IQProvider, and also your CustomIQ need to implement the correct output of the xml. You are checking for a tag, where clearly it doesn't exist, so this parser will never work. Assuming this is the tag you want to parse

<jid username='xx' fullname='yy'>xx@aa.bb.cc.dd/Resource</jid>

I didn't tested but this should work, your parseIQ method should look like this

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;
}

And your CustomIQ class:

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();
  }

}
查看更多
登录 后发表回答