发送自定义 IQ 查询 (Android)(Smack) [英] Sending a custom IQ query (Android)(Smack)

查看:46
本文介绍了发送自定义 IQ 查询 (Android)(Smack)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果的格式是这个.

<iq from='52@localhost' to='20@localhost/Gajim' id='253' type='result'>
<query xmlns='someName'>
<item subscription='both' jid='1@localhost'/>
</query>
</iq>

我正在尝试使用以下格式发送自定义 iq 查询.

I am trying to send a custom iq query with the following format.

 <iq xmlns="Name" type="get" id="253">
    <query xmlns="someName">
    <auth type='token'>asd</auth>
    </query>
    </iq>

由此我明白我需要发送一个带有授权类型令牌(令牌 id )的查询.这是我的尝试.

From this I understand that I need to send a query with a authorization type token(token id ). Here is my try at that.

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 查询吗?有人可以演示一下怎么做吗?

I am confused on how to complete this getChildElementXML() and furthermore I get an error when I try to instantiate a new IQ because I need to implement some builder method. Should I create a new class for sending custom IQ queries?Can someone show how to do it?

注意:建设性的反馈是值得赞赏的,如果有人指出歧义,我可以使问题更清楚.

Note: Constructive feedback is appreciated, I can make the question clearer if someone points out an ambiguity.

推荐答案

这将回答您的问题,但请记住,下一步您将需要这样的东西:使用 aSmack 客户端映射 Openfire 自定义插件

一般来说,ID是smack API创建的,不值得你手动分配.

Generally speaking, ID it's created by smack API and you don't deserve to assign it manually.

一般来说,xmnls 但要分配给自定义标签而不是 IQ 本身.

Generally speaking, xmnls but to be assigned to custom tag and not IQ itself.

我们的目标:

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

这篇关于发送自定义 IQ 查询 (Android)(Smack)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆