Android没有使用ez-vcard从VCARD获取正确的街道地址

时间:2015-07-24 10:17:24

标签: java android vcard

我使用ez-vcard(https://github.com/mangstadt/ez-vcard)来解析VCARD字符串。例如,VCARD字符串之一是

"BEGIN:VCARD\n" +
"N:Mandal;Saroj\n" +
"FN:Saroj Mandal\n" +
"ORG:xyz\n" +
"TITLE:Advisory Software Engineer\n" +
"TEL;WORK:022-123456\n" +
"TEL;CELL:1234567890\n" +
"TEL;FAX:1234567890\n" +
"ADR;ADDRESS:;;D-100, Tech Heights, Sainikwadi, Wadgaonshri;Pune;Maharashtra;411014;India\n" +
"LABEL;ADDRESS;ENCODING=QUOTED-PRINTABLE:D-100,Tech Heights, Sainikwadi, Wadgaonshri=0D=0Pune, Maharashtra=0D=0411014=0D=0India\n" +
"EMAIL;PREF;INTERNET:hificoders@gmail.com\n" +
"URL;WORK:http://www.example.com\n" +
"END:VCARD";

在android中我想从上面的示例VCARD字符串中获取街道地址 我使用了下面的代码

for(Address address :vcard.getAddresses()){
   Log.d(TAG, "Street address" + address.getStreetAddress());
}

但是我只能将D-100作为街道地址,它应该是D-100,Tech Heights,Sainikwadi,Wadgaonshri,但如果我从VCARD上方删除逗号(,),那么我会得到整个字符串。请给出一些解决方案。

1 个答案:

答案 0 :(得分:1)

必须在ADR属性中转义逗号字符。在所有逗号之前加上反斜杠。

"ADR;ADDRESS:;;D-100\, Tech Heights\, Sainikwadi\, Wadgaonshri;Pune;Maharashtra;411014;India\n" +

另外,为什么你的属性中有一个没有值的“ADDRESS”参数?请删除它。

"ADR:;;D-100\, Tech Heights\, Sainikwadi\, Wadgaonshri;Pune;Maharashtra;411014;India\n" +

编辑(2015年7月27日):

要回答评论中的问题:

要强制解析器将逗号视为普通字符,您可以注册一个自定义划线类,该类扩展标准AddressScribe划线并覆盖其解析功能:

VCardReader reader = new VCardReader(...);
reader.getScribeIndex().register(new AddressScribe() {
    @Override
    protected Address _parseText(String value, VCardDataType dataType, VCardVersion version, VCardParameters parameters, List<String> warnings) {
        String components[] = value.split(";");
        int i = 0;

        Address property = new Address();
        property.setPoBox(next(components, i++));
        property.setExtendedAddress(next(components, i++));
        property.setStreetAddress(next(components, i++));
        property.setLocality(next(components, i++));
        property.setRegion(next(components, i++));
        property.setPostalCode(next(components, i++));
        property.setCountry(next(components, i++));
        return property;
    }

    private String next(String components[], int index) {
        if (index >= components.length) {
            return null;
        }
        String next = components[index];
        return (next.length() > 0) ? next : null;
    }
});