asmak 数据包侦听器和自定义 IQProvider 未触发/调用

Posted

技术标签:

【中文标题】asmak 数据包侦听器和自定义 IQProvider 未触发/调用【英文标题】:asmak packet listener and custom IQProvider not triggering / called 【发布时间】:2013-07-05 09:07:09 【问题描述】:

我在一个 android 项目中使用 asmack 最新版本 (asmack-android-8-source-0.8.3),我试图通过发送多个 IQ 并接收响应来与服务器通信。 我收到响应并且它被正确解析但它没有触发包侦听器。 我有以下代码:

vCard_IQProvider.java

public class vCard_IQProvider implements IQProvider

    public static final String NAMESPACE = "vcard-temp";
    public static final String ELEMENT_NAME = "vCard";
    public static final String LAST_STATUS = "LAST_STATUS";
    public static final String LAST_STATUS_DESCRIPTION = "LAST_STATUS_DESCRIPTION";
    public static final String WORK_TIME_RANGE = "WORK_TIME_RANGE";
    public static final String SOUND_SETTINGS = "SOUND_SETTINGS";
    public static final String AUTO_LOCATION_MACHINE_DATA = "AUTO_LOCATION_MACHINE_DATA";
    public static final String AUTO_LOCATION = "AUTO_LOCATION";
    public static final String AUTO_LOCATION_ENABLED = "AUTO_LOCATION_ENABLED";
    public static final String AUTO_ONLINE = "AUTO_ONLINE";
    public static final String HIDEACTIONNOTIFICATIONS = "HideActionNotifications";
    public static final String AUTO_CONNECT = "AUTO_CONNECT";
    public static final String AUTO_OFFLINE_WORK_TIME = "AUTO_OFFLINE_WORK_TIME";
    public static final String AUTO_RELOGIN = "AUTO_RELOGIN";
    public static final String CONNECTED_VIA_INTERNET = "CONNECTED_VIA_INTERNET";
    public static final String MINIMIZE_CHAT = "MINIMIZE_CHAT";
    public static final String PROMPT_PROJECT_SWITCH = "PROMPT_PROJECT_SWITCH";
    public static final String MACHINE_NAME = "MACHINE_NAME";
    public static final String MROFFICE_VER = "MROFFICE_VER";
    public static final String WORK = "WORK";
    public static final String LOCALITY = "LOCALITY";
    public static final String TIMESTAMP = "timestamp";
    public static final String REGION = "REGION";
    public static final String EXT = "EXT";
    public static final String LAST_ACTIVITY_TS = "LAST_ACTIVITY_TS";
    public static final String FUTURE_STATUS = "FUTURE_STATUS";
    public static final String FUTURE_STATUS_DESCRIPTION = "FUTURE_STATUS_DESCRIPTION";
    public static final String FUTURE_STATUS_TS = "FUTURE_STATUS_TS";
    public static final String CUSTOM = "CUSTOM";
    public static final String PREF = "PREF";
    private Map<String, String> list = new HashMap<String, String>();

    @Override
    public IQ parseIQ(XmlPullParser parser) throws Exception
    
        String name;
        boolean isEmpty;
        boolean done = false;

        while(parser.next() != XmlPullParser.END_DOCUMENT && false == done)
        
            name = parser.getName();

            switch (parser.getEventType())
            
                case XmlPullParser.START_TAG:
                
                    isEmpty = parser.isEmptyElementTag();

                    if(name.equalsIgnoreCase(LAST_STATUS) && false == isEmpty)
                    
                        list.put(LAST_STATUS, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(LAST_STATUS_DESCRIPTION) && false == isEmpty)
                    
                        list.put(LAST_STATUS_DESCRIPTION , parser.nextText());
                    
                    else if(name.equalsIgnoreCase(WORK_TIME_RANGE) && false == isEmpty)
                    
                        list.put(WORK_TIME_RANGE, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(SOUND_SETTINGS) && false == isEmpty)
                    
                        list.put(SOUND_SETTINGS, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(AUTO_LOCATION_MACHINE_DATA) && false == isEmpty)
                    
                        list.put(AUTO_LOCATION_MACHINE_DATA, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(AUTO_LOCATION) && false == isEmpty)
                    
                        list.put(AUTO_LOCATION, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(AUTO_LOCATION_ENABLED) && false == isEmpty)
                    
                        list.put(AUTO_LOCATION_ENABLED, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(AUTO_ONLINE) && false == isEmpty)
                    
                        list.put(AUTO_ONLINE, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(HIDEACTIONNOTIFICATIONS) && false == isEmpty)
                    
                        list.put(HIDEACTIONNOTIFICATIONS, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(AUTO_CONNECT) && false == isEmpty)
                    
                        list.put(AUTO_CONNECT, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(AUTO_OFFLINE_WORK_TIME) && false == isEmpty)
                    
                        list.put(AUTO_OFFLINE_WORK_TIME, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(AUTO_RELOGIN) && false == isEmpty)
                    
                        list.put(AUTO_RELOGIN, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(CONNECTED_VIA_INTERNET) && false == isEmpty)
                    
                        list.put(CONNECTED_VIA_INTERNET, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(MINIMIZE_CHAT) && false == isEmpty)
                    
                        list.put(MINIMIZE_CHAT, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(PROMPT_PROJECT_SWITCH) && false == isEmpty)
                    
                        list.put(PROMPT_PROJECT_SWITCH, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(MACHINE_NAME) && false == isEmpty)
                    
                        list.put(MACHINE_NAME, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(MROFFICE_VER) && false == isEmpty)
                    
                        list.put(MROFFICE_VER, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(WORK) && false == isEmpty)
                    
                        list.put(WORK, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(LOCALITY) && false == isEmpty)
                    
                        list.put(LOCALITY, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(TIMESTAMP) && false == isEmpty)
                    
                        list.put(TIMESTAMP, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(REGION) && false == isEmpty)
                    
                        list.put(REGION, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(EXT) && false == isEmpty)
                    
                        list.put(EXT, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(LAST_ACTIVITY_TS) && false == isEmpty)
                    
                        list.put(LAST_ACTIVITY_TS, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(FUTURE_STATUS) && false == isEmpty)
                    
                        list.put(FUTURE_STATUS, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(FUTURE_STATUS_DESCRIPTION) && false == isEmpty)
                    
                        list.put(FUTURE_STATUS_DESCRIPTION, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(FUTURE_STATUS_TS) && false == isEmpty)
                    
                        list.put(FUTURE_STATUS_TS, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(CUSTOM) && false == isEmpty)
                    
                        list.put(CUSTOM, parser.nextText());
                    
                    else if(name.equalsIgnoreCase(PREF) && false == isEmpty)
                    
                        list.put(PREF, parser.nextText());
                    

                    break;
                
                case XmlPullParser.END_TAG:
                
                    done = ELEMENT_NAME.equalsIgnoreCase(name);
                    break;
                
            
        
        name = null;

        return new vCard_IQ(list);
    

vCard_IQ.java

public class vCard_IQ extends IQ

    public static final String ID = "vcard";
    private static Map<String, String> list;
    private static boolean finishedParsing = false;

    public vCard_IQ(Map<String, String> l)
    
        if(null == list)
        
            list  = new HashMap<String, String>();
        

        list.clear();
        list.putAll(l);

        finishedParsing = true;
    

    @Override
    public String getChildElementXML()
    
        return null;
    

    public static final Map<String, String> getData()
    
        return list;
    

    public static void setFinishedParsingToFalse()
    
        finishedParsing = false;
    

    public static final boolean finishedParsing()
    
        return finishedParsing;
    

我添加提供者:

ProviderManager.getInstance().addIQProvider(vCard_IQProvider.ELEMENT_NAME, vCard_IQProvider.NAMESPACE, new vCard_IQProvider());

和包监听器:

connection.addPacketListener(new PacketListener()

    @Override
    public void processPacket(Packet p)
       
        if(p.getPacketID().equals(vCard_IQ.ID))
        
            vCard_IQ pp = (vCard_IQ)p;

            //access the parsed data
            //vCard_IQ.getData().get.......

            pp = null;
        
    
, 
    new PacketFilter()
    
        @Override
        public boolean accept(Packet arg0)
        
            return true;
        
    ); 

数据包过滤器设置为接受所有数据包,侦听器由于某种原因未触发。我可以在调试器中看到服务器正在发送响应。

我什至试图通过创建一个异步任务并在后台线程中等待来绕过侦听器,直到解析响应并访问它。现在它仅适用于发送的第一个 iq - 我收到一个响应并且它被正确解析,但对于其余部分,我可以在调试器中看到它正在发送响应的服务器,但它从未到达解析器。从未调用过的解析器。

   Asynk<Void, Void, Void> asynk = new Asynk<Void, Void, Void>()
    
        Packet iq_vcard;

        @Override
        protected Void doInBackground(Void... params)
        
            for(String s : names_list)
            
                    final String name = s;
                    iq_vcard = new Packet()
                    
                        @Override
                        public String toXML()
                        
                            String str = String.format("<iq from='%s' to='%s' type='get' id='" + vCard_IQ.ID + "'><vCard xmlns='vcard-temp'/></iq>",
                                            sharedPrefs.getString(LogIn.USERNAME, "") + "@" + sharedPrefs.getString(Settings_LogIn.DOMAIN, Settings_LogIn.ERROR) + "/iOffice",
                                            name + "@" + sharedPrefs.getString(Settings_LogIn.DOMAIN, Settings_LogIn.ERROR));

                            Log.e("iq_vcard", str);
                            return str;
                        
                    ;
                    connection().sendPacket(iq_vcard);
                    iq_vcard = null;

                    while(false == vCard_IQ.finishedParsing())
                    
                        try
                        
                            Thread.sleep(1000);
                            Log.e("TAG", "waiting to finish parsing...");
                        
                        catch (InterruptedException e)
                        
                            e.printStackTrace();
                        
                    

                    //access the parsed data
                    //vCard_IQ.getData().get.......

                    vCard_IQ.setFinishedParsingToFalse();
            

            return null;
        

        @Override
        protected void onPostExecute(Void result)
        

        
    ;
    asynk.execute();

有什么建议吗?

【问题讨论】:

我不确定我是否正确,但我在应用程序中观察到,当您启动 IQ 提供程序时,其他数据包侦听器无法正常工作。所有类型的传出或传入数据包仅由 IQ 提供程序处理。尝试在 IQ 提供程序中记录所有数据包,然后你就会知道。 【参考方案1】:

这是 aSmack 中的一个错误,经过多次尝试解决此问题后,我自己按照 tutorial 编译 aSmack 的尝试失败并尝试了我能想到的任何事情,我做了以下事情:我在我的android 项目并在那里转储了所有 aSmack 的 java 类。这样我就让 eclipse 编译了 aSmack,我可以更好地控制它的代码。

问题是每次我从服务器收到自定义 iq 响应时,在 PacketParserUtils.java 中,aSmack 都会返回错误的 elementName 和命名空间,从而选择错误的 iq 提供程序来解析响应。

【讨论】:

【参考方案2】:

来自文档: http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smack/provider/IQProvider.html

在方法调用结束时,解析器必须定位在 子元素的结束标签。

【讨论】:

【参考方案3】:

如果有人试图做同样的事情,我发现了问题所在。

vCard_IQProvider类的while条件错误,应该是:

while(!done && parser.next() != XmlPullParser.END_DOCUMENT)

否则当 done 设置为 true 时,while 将再次检查条件并将解析器移动到下一个元素(调用 parser.next())。

【讨论】:

以上是关于asmak 数据包侦听器和自定义 IQProvider 未触发/调用的主要内容,如果未能解决你的问题,请参考以下文章

使用Advanced Installer制作IIS安装包(二:配置安装包依赖项和自定义dll)

扩展 Sonata 用户包和自定义操作

go语言基础之包和自定义包与main包

如何使用 C# 中的 Nuget 包在谷歌分析中创建自定义维度和自定义指标

maven-shade-plugin 和自定义包装类型

thinkphp5 验证码和自定义路由