gpt4 book ai didi

java - 如何从 vcard 中获取电话号码

转载 作者:行者123 更新时间:2023-11-30 11:41:51 27 4
gpt4 key购买 nike

我正在使用 cardme 库来处理电子名片。以下是我的代码

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 电话,不会返回任何其他内容。甚至循环也没有停止。请帮忙!

最佳答案

我能看见

TelephoneFeature.getTelephone()

我还建议看一下

TelephoneFeature.getTelephoneParameterTypes()

查看类型

更新

小心迭代器

每次调用 vcard.getTelephoneNumbers() 都会创建一个新的 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());

}

到上一个循环,你应该得到你要找的东西了

关于java - 如何从 vcard 中获取电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12097357/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com