2

我正在使用 cardme 库来处理 vcards。以下是我的代码

package vcardtest;

import java.io.*;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.cardme.engine.VCardEngine;
import net.sourceforge.cardme.vcard.VCard;
import net.sourceforge.cardme.vcard.features.EmailFeature;
import net.sourceforge.cardme.vcard.features.NameFeature;
import net.sourceforge.cardme.vcard.features.NicknameFeature;
import net.sourceforge.cardme.vcard.features.TelephoneFeature;
import net.sourceforge.cardme.vcard.types.parameters.TelephoneParameterType;

public class VCardTest 
{

    public static void main(String[] args) 
    {
        File vcardFile = new File("C:/Users/yohan/Contacts/Yohan Weerasinghe.vcf");
        VCardEngine vcardEngine = new VCardEngine();
        try 
        {
            VCard vcard = vcardEngine.parse(vcardFile);
            String name = vcard.getName().getGivenName();

            EmailFeature email = vcard.getEmails().next();
            String sEmail = email.getEmail();

            NicknameFeature nickName = vcard.getNicknames();
            Iterator<String> nicknames = nickName.getNicknames();
            String sNickName = nicknames.next();

            Iterator<TelephoneFeature> telephoneNumbers = vcard.getTelephoneNumbers();
            TelephoneFeature next = telephoneNumbers.next();

            String telephone = "";


                while(vcard.getTelephoneNumbers().hasNext())
                {
                    TelephoneFeature next1 = vcard.getTelephoneNumbers().next();
                    telephone = next1.getTelephone();



                    System.out.println(telephone);
                }



            Iterator<TelephoneParameterType> telephoneParameterTypes = next.getTelephoneParameterTypes();
            TelephoneParameterType next1 = telephoneParameterTypes.next();
            String type = next1.getType();

            TelephoneParameterType next2 = telephoneParameterTypes.next();
            String type2 = next2.getType();


            System.out.println( name );
            System.out.println(sEmail);
            System.out.println(sNickName);

            System.out.println(type);
            System.out.println(type2);


        } catch (IOException ex) 
        {
           ex.printStackTrace();
        }
    }
}

但是,没有称为 getNumber() 之类的方法。如何获取手机号码和地号?请帮忙!

注意:我更新了代码。在那里,你可以看到我设法得到了电话号码。但是,这只会返回 HOME 电话,不会返回任何其他内容。甚至循环也没有停止。请帮忙!

4

1 回答 1

1

我可以看到

TelephoneFeature.getTelephone()

我也建议看看

TelephoneFeature.getTelephoneParameterTypes()

查看类型

更新

小心迭代器

每次调用 vcard.getTelephoneNumbers() 都会创建一个 new Iterator,这意味着您可能会陷入无限循环。

Iterator<TelephoneFeature> itNumbers = vcard.getTelephoneNumbers();
while (itNumbers.hasNext()) {

    TelephoneFeature next1 = itNumbers.next();
    String telephone = next1.getTelephone();

    System.out.println(telephone);
    System.out.println("types = " + next1.getExtendedTelephoneParameterSize());

    Iterator<XTelephoneParameterType> itTypes = next1.getExtendedTelephoneParameterTypes();
    while (itTypes.hasNext()) {

        XTelephoneParameterType next = itTypes.next();
        System.out.println(" - " + next.getType() + " / " + next.getDescription());

    }

}

我已经更正了,问题(不是错误)出在测试仪上,而不是 API 上:P

如果你添加

Iterator<TelephoneParameterType> itNTypes = next1.getTelephoneParameterTypes();
while (itNTypes .hasNext()) {

    TelephoneParameterType next = itNTypes .next();
    System.out.println(" - " + next.getType() + " / " + next.getDescription());

}

到上一个循环,你应该得到你正在寻找的东西

于 2012-08-23T20:14:09.457 回答