【问题标题】:How to get IQ tag using smack Java?如何使用 smack Java 获取 IQ 标签?
【发布时间】:2012-03-18 14:48:50
【问题描述】:

实际上,问题是当我的xmpp客户端发送朋友邀请,然后收件人已经批准邀请时,openfire服务器再次向发起者/邀请发送者推送订阅数据包以进行授权,这就是我想要阻止的原因这是通过使用 IQ 标签自动过滤它,然后自动授权它。

但是使用 PacketListener,我无法获得 IQ 标签...

我该怎么做?

@Override
public void processPacket(Packet packet) {
    Log.i(TAG, "SECOND subscription");
    Log.d(TAG, "SECOND: "+packet.toXML());
    if (packet instanceof Presence) {
        Presence p = (Presence) packet;
        Log.d(TAG, "TYPE-Presence: "+p.getType());
        if (p.getType() != Presence.Type.subscribe)
        return;
        String from = p.getFrom();
        Log.d(TAG, "PACKET from: "+from);
        Notification notification = new Notification(android.R.drawable.stat_notify_more, mService.getString(
                R.string.AcceptContactRequest, from), System.currentTimeMillis());
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        Intent intent = new Intent(mService, Subscription.class);
        intent.setData(Contact.makeXmppUri(from));
        notification.setLatestEventInfo(mService, from, mService
                .getString(R.string.AcceptContactRequestFrom, from), PendingIntent.getActivity(mService, 0,
                        intent, PendingIntent.FLAG_ONE_SHOT));
        int id = p.hashCode();
        mService.sendNotification(id, notification);
    }
}

【问题讨论】:

    标签: java xmpp smack


    【解决方案1】:

    可以使用“IQTypeFilter”过滤器过滤掉传入的 IQ。这是说明该方法的示例代码。

        connection.connect();
    
        /* packet listener: listen for incoming messages of type IQ on the connection (whatever the buddy) */
        PacketFilter filter = new IQTypeFilter(IQ.Type.SET); // or IQ.Type.GET etc. according to what you like to filter. 
    
        connection.addPacketListener(new PacketListener() { 
            public void processPacket(Packet packet) {
                // HERE YOU PUT YOUR CODE TO HANDLE THE IQ MESSAGE
            }
        }, filter);  
    

    【讨论】:

    • 您能否详细说明您的答案。其实我不知道如何处理 IQ 消息
    【解决方案2】:

    您可以使用 IQTypeFilter 来实现它,它是 IQ 数据包类型的过滤器:

    public final class IQTypeFilter extends FlexibleStanzaTypeFilter<IQ> {
    
        public static final StanzaFilter GET = new IQTypeFilter(Type.get);
        public static final StanzaFilter SET = new IQTypeFilter(Type.set);
        public static final StanzaFilter RESULT = new IQTypeFilter(Type.result);
        public static final StanzaFilter ERROR = new IQTypeFilter(Type.error);
        public static final StanzaFilter GET_OR_SET = new OrFilter(GET, SET);
    
        private final IQ.Type type;
    
        private IQTypeFilter(IQ.Type type) {
            super(IQ.class);
            this.type = Objects.requireNonNull(type, "Type must not be null");
        }
    
        @Override
        protected boolean acceptSpecific(IQ iq) {
            return iq.getType() == type;
        }
    
        @Override
        public String toString() {
            return getClass().getSimpleName() + ": type=" + type;
        }
    

    【讨论】:

      【解决方案3】:

      正如 Javadoc 中定义的那样,IQTypeFilter 是 IQ 数据包类型的过滤器。仅当数据包是 IQ 数据包并且它与构造函数中提供的类型匹配时才返回 true。 the use of IQTypeFilter有一些例子

      【讨论】:

        【解决方案4】:

        这是使用 Smack 4.3.4 的更新答案。为了使我的代码清晰,我明确放置了 cmets。

           /**
             *  packet listener: listen for incoming messages of whith IQ as elelement
             *  exmaple of IQ : <iq from="mbula@domain" to="dedi@domain" type="get" >.......</iq>
             */
            public static void listenToStanzas(AbstractXMPPConnection connection){
        
                // IQ filter type. it can Be GET, SET, RESULT, ERROR
                //in my case I filter SET IQs
                StanzaFilter filter =  IQTypeFilter.SET;
        
                connection.addSyncStanzaListener(new StanzaListener() {
                    @Override
                    public void processStanza(Stanza packet) {
                        //Put yoour code Here
                    }
                }, filter);
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-10-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多