本文介绍了如何解析在ASMACK一个CustomIQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASMACK库FO我的申请。我从我的服务器收到了以下的智商。

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'>[email protected]/Resource</jid></bind></iq>

有关解析这智商,我加入了IQProvider同时使用的ProviderManager

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

和我的 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

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

推荐答案

也许有一个供应商已经报名参加这个命名空间?
也许你需要做的XML的完整和正确解析你的IQProvider,而且你CustomIQ需要实现的XML的正确的输出。
你检查标签,在这里显然不存在,所以这个解析器不会有任何效果。
假设这是你想要解析的标签

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'>[email protected]/Resource</jid>

我没有测试,但这应该工作,你的parseIQ方法应该是这样的。

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

和您的CustomIQ类:

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

}

这篇关于如何解析在ASMACK一个CustomIQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 04:25