如何使用 Smack 4 创建空 IQ 数据包

How to create Empty IQ Packet using Smack 4

我正在使用 Smack 4 开发呼叫信号。我想使用 Smack 4 和 Java:

发送具有以下格式的 IQ 结果
<iq from="sister@realworld.lit/home"
id="v73hwcx9"
to="alice@wonderland.lit/rabbithole"
type="result"/>

这是我做过但没做的事

public class AcknowledgeIQ extends IQ {

    public AcknowledgeIQ() {
        super("");
        setType(Type.result);
    }

    @Override
    public IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
        return null;
    }

    public String toXml(){
        return super.toXML(null).toString();
    }
}

我已经在 SMACK 4.3.4 上测试了以下代码。

public class AcknowledgeIQ extends org.jivesoftware.smack.packet.IQ {

    public AcknowledgeIQ() {
        super(null, null);
    }

    @Override
    public IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
        xml.rightAngleBracket();
        return xml;
    }
}

用法示例:

AcknowledgeIQ ackIQ = new AcknowledgeIQ();
ackIQ.setType(org.jivesoftware.smack.packet.IQ.Type.result);
Log.d(TAG, "AcknowledgeIQ = "+ackIQ.toXML(null).toString());

Logcat 输出:

AcknowledgeIQ = <iq xmlns='jabber:client' to='alice@wonderland.lit/rabbithole' id='MGr5o-5' type='result'></iq>