Get customized element from `vcard` using `xmpp` and `smack`

时间:2019-03-05 16:39:21

标签: android xmpp smack

I created a vcard with custom elements:

<vCard xmlns='vcard-temp'><NICKNAME>nickname</NICKNAME><elementName xmlns='xmlns:custom'><avatar>elementName_avatar_3.png</avatar></elementName></vCard>

when however I ask this vcard, my result is:

<iq xmlns='jabber:client' to='602157@peropenfire101.per.local/3r6td85ng7' from='271087@peropenfire101.per.local' id='jA5pI-33' type='result'><vCard xmlns='vcard-temp'><JABBERID>271087@peropenfire101.per.local/4usgw7j7u</JABBERID><NICKNAME>nickname</NICKNAME></vCard></iq>

as you can see, the custom elements are missing. this is my request:

try {
        connection.sendStanzaWithResponseCallback(
                new cdcd(from),
                new StanzaFilter() {
                    @Override
                    public boolean accept(Stanza stanza) {
                        return false;
                    }
                },
                new StanzaListener() {
                    @Override
                    public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException, SmackException.NotLoggedInException {
                    }
                });
    } catch (Exception e) {
        e.printStackTrace();
    }

private class cdcd extends Stanza{

    private final Jid from;

    public cdcd(Jid from) {
        this.from = from;
    }

    @Override
    public String toString() {
        return null;
    }

    @Override
    public CharSequence toXML(String enclosingNamespace) {
        return "<iq from=\'"+ from +"\' id='v1' type='get'><vCard xmlns='vcard-temp'/></iq>";
    }
}

I don't know how fix this.

2 个答案:

答案 0 :(得分:0)

根据XEP-054,不支持vCard DTD的非标准元素的功能,因此您的XMPP服务器将其忽略。

  

不幸的是,道森的提案并未在IETF的标准流程中向前发展。由于现在迷失在时间的迷雾中,Jabber项目继续使用draft-dawson-vcard-xml-dtd-01中的DTD,进行了两个小修改以使其适合Jabber社区使用(添加了JABBERID和DESC元素),但也要在所有大写字母中指定元素名称,而不是在draft-dawson-vcard-xml-dtd-01中定义的小写字母。此外,Jabber社区遵循该草案中有关版本信息的用法(但不包括DTD),包括将其用作vCard元素的属性而不是子元素。这种格式是在Jabber社区的“ vcard-temp”命名空间下实现的。

您可以看到here所有受支持的元素。 另请参见讨论here

答案 1 :(得分:0)

已解决... 我创建了一个扩展f(type: types[0]) // ERROR: Cannot invoke 'f' with an argument list of type '(type: P.Type)' 的新类,并在覆盖的方法parse中解析了自定义标签

VCardProvider

别忘了从原始提供者那里复制所有解析器,并在身份验证后添加public class CustomVCardProvider extends VCardProvider { private static final String[] TEL = new String[]{ "VOICE", "FAX", "PAGER", "MSG", "CELL", "VIDEO", "BBS", "MODEM", "ISDN", "PCS", "PREF", }; private static final String[] ADR = new String[]{ "POSTAL", "PARCEL", "DOM", "INTL", "PREF", "POBOX", "EXTADR", "STREET", "LOCALITY", "REGION", "PCODE", "CTRY", "FF", }; @Override public VCard parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { VCard vCard = new VCard(); String name = null; outerloop: while (true) { int eventType = parser.next(); switch (eventType) { case XmlPullParser.START_TAG: name = parser.getName(); switch (name) { case CUSTOM_TAG: parseCustomTag(parser, vCard); break; case "N": parseName(parser, vCard); break; case "ORG": parseOrg(parser, vCard); break; case "TEL": parseTel(parser, vCard); break; case "ADR": parseAddress(parser, vCard); break; case "EMAIL": parseEmail(parser, vCard); break; case "NICKNAME": vCard.setNickName(parser.nextText()); break; case "JABBERID": vCard.setJabberId(parser.nextText()); break; case "PHOTO": parsePhoto(parser, vCard); break; default: break; } break; case XmlPullParser.TEXT: if (initialDepth + 1 == parser.getDepth()) { vCard.setField(name, parser.getText()); } break; case XmlPullParser.END_TAG: if (parser.getDepth() == initialDepth) { break outerloop; } break; default: break; } } return vCard; }