结果的格式将是这样。
<iq from='52@localhost' to='20@localhost/Gajim' id='253' type='result'>
<query xmlns='someName'>
<item subscription='both' jid='1@localhost'/>
</query>
</iq>我试图用以下格式发送一个定制的iq查询。
<iq xmlns="Name" type="get" id="253">
<query xmlns="someName">
<auth type='token'>asd</auth>
</query>
</iq>从这一点我了解到,我需要发送一个带有授权类型令牌(Token id )的查询。这是我的尝试。
final IQ iq = new IQ() {
@Override
public String getChildElementXML() {
return "<query xmlns='someName'auth type="+t_id"+"asd<................'</query>"; // I am confused on how to write here
}
};
iq.setType(IQ.Type.get);
connection.sendPacket(iq); // connection is an XMPPTCPConnection object.我对如何完成这个getChildElementXML()感到困惑,而且当我试图实例化一个新的IQ时会出错,因为我需要实现一些构建器方法。我应该创建一个新的类来发送自定义的IQ查询吗?有人能告诉我怎么做吗?
注意:有建设性的反馈很感激,如果有人指出歧义,我可以把问题弄得更清楚。
发布于 2016-12-28 12:57:07
这将回答您的问题,但请记住,在下一步您需要这样的东西:Mapping Openfire Custom plugin with aSmack Client
一般来说,ID是由smack创建的,您不应该手动分配它。
一般来说,xmnls只是分配给定制标签,而不是IQ本身。
我们的目标:
<iq from="me@domain" to="domain" type="get" id="253">
<query xmlns="someName">
<auth type='token'>asd</auth>
</query>
</iq>你的课会是什么样子:
package ....;
import org.jivesoftware.smack.packet.IQ;
public class IQCustomAuth extends IQ
{
public final static String childElementName = "query";
public final static String childElementNamespace = "com:prethia:query#auth";
private final String auth;
private final String typeAuth;
public IQCustomAuth(String userFrom, String server, String typeAuth, String auth)
{
super( childElementName, childElementNamespace );
this.setType( IQ.Type.get );
this.auth = auth;
this.typeAuth = typeAuth;
setTo( server );
setFrom( userFrom );
}
@Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder( IQChildElementXmlStringBuilder xml )
{
xml.rightAngleBracket();
xml.halfOpenElement( "auth ");
xml.attribute( "type", this.typeAuth );
xml.rightAngleBracket();
xml.append(auth);
xml.closeElement("auth");
return xml;
}
}测试:
IQCustomAuth iq = new IQCustomAuth( "me@domain", "domain", "token", "asd" );
System.out.println(iq.toString());发送:
connection.sendPacket(new IQCustomAuth( "me@domain", "domain", "token", "asd" ));https://stackoverflow.com/questions/41323363
复制相似问题