- 發送數據
在org.jivesoftware.smackx.packet.VCard 中構建 字段
以desc爲例
public String desc; //好友簽名
public String getDesc() {
return otherSimpleFields.get("DESC");
}
public void setDesc(String desc) {
otherSimpleFields.put("DESC", desc);
}
在otherSimpleFields 添加名爲” otherSimpleFields”節點
程序 會將 otherSimpleFields集合中字段生成xmpp協議xml
調用VCard的save方法即可以保存個人信息
2.解析獲取數據
收到服務器返回的數據後 會調用packetReader解析數據
解析數據的工具類org.jivesoftware.smack.util.PacketParserUtils 在類中分別有解析message、iq、presence等數據的解析方法
Vcard屬於iq 在parseIQ中處理
parseIQ 方法中調用IQProvider parse解析數據
具體執行在繼承了IQProvider的類 org.jivesoftware.smackx.provider.vCardProvider中createVCardFromXML中將xml解析成vcard的
爲了讓createVCardFromXML 中 new VCardReader(vCard, document).initializeFields();
爲了讓VCardReader能識別讀取增加的”DESC”字段,需要在initializeFields方法中設置
vCard.setDesc(getTagContents("DESC")) ;
這樣vcard就可以讀取和解析自定義的字段了