java 如何使用 Smack 使用 XMPP 设置/获取配置文件数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1838770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set/get profile data with XMPP using Smack
提问by Ulrich Scheller
I am working on a XMPP client on Android, using the Smack library. The roster/messaging/presence stuff is running very well. However, I didn't find a way to store additional profile information (userpicture, the dogs name, ...).
我正在使用 Smack 库在 Android 上开发 XMPP 客户端。花名册/消息传递/在线状态运行良好。但是,我没有找到存储其他个人资料信息的方法(用户图片、狗的名字……)。
The only way I see from googling is using VCards. But it simply did not work. I tried the following:
我从谷歌上看到的唯一方法是使用 VCard。但它根本不起作用。我尝试了以下方法:
VCard vCard = new VCard();
vCard.load(connection);
vCard.setEmailHome("[email protected]");
vCard.setLastName("Scheller");
vCard.setField("blafasel", "asdf");
vCard.save(connection);
Then I was looking for a way to see that VCard information. It did neither show up in iChat nor in this System.out:
然后我正在寻找一种方法来查看该 VCard 信息。它既没有出现在 iChat 中,也没有出现在这个 System.out 中:
vCard.load(connection, user);
System.out.println(user + " has this vCard: " + vCard.toXML());
So anything went wrong, but theres no indication what it was. I tried this with the google talk server and my own copy of openfire with the same result. Btw, I am using this version of Smack: http://davanum.wordpress.com/2007/12/31/android-just-use-smack-api-for-xmpp/
所以什么都出错了,但没有迹象表明它是什么。我用 google talk 服务器和我自己的 openfire 副本尝试了这个,结果相同。顺便说一句,我正在使用这个版本的 Smack:http: //davanum.wordpress.com/2007/12/31/android-just-use-smack-api-for-xmpp/
What am I doing wrong here? What is the correct way of storing profile related information with Smack?
我在这里做错了什么?使用 Smack 存储配置文件相关信息的正确方法是什么?
回答by Ulrich Scheller
I have checked out the source of Smack and went through the important parts with a debugger, as well as using the Smack Debug Window. The problem is inside the VCard implementation of the Smack API. Saving a VCard does work as described, however the loading is broken.
我已经检查了 Smack 的源代码并使用调试器浏览了重要部分,以及使用 Smack 调试窗口。问题出在Smack API的 VCard 实现中。保存 VCard 确实如所述那样工作,但是加载被破坏了。
parseIQ(XmlPullParser parser) is part of the PacketReader.java class and handles different types of packages. It only handles tags with the following namespaces:
parseIQ(XmlPullParser parser) 是 PacketReader.java 类的一部分,处理不同类型的包。它只处理具有以下命名空间的标签:
"jabber:iq:auth", "jabber:iq:roster", "jabber:iq:register", "urn:ietf:params:xml:ns:xmpp-bind"
"jabber:iq:auth", "jabber:iq:roster", "jabber:iq:register", "urn:ietf:params:xml:ns:xmpp-bind"
It also looks if there is any registered IQProvider in the ProviderManager. And this is the root of my problem. There is no IQProvider for VCards registered. So whatever information is inside of the vCard tag simply gets dropped.
它还查看 ProviderManager 中是否有任何已注册的 IQProvider。这就是我问题的根源。没有注册 VCard 的 IQProvider。因此,vCard 标签内的任何信息都会被删除。
It is not too hard to register this IQProvider though:
不过,注册这个 IQProvider 并不太难:
ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp", new VCardProvider());
This solved my little example above for saving my own vCard and downloading it again. I am still having trouble with downloading other users vcards... Gonna have a closer look into this and maybe open up another thread for that issue.
这解决了我上面保存我自己的 vCard 并再次下载的小例子。我在下载其他用户的 vcard 时仍然遇到问题......我会仔细研究一下这个问题,也许会为这个问题打开另一个线程。
回答by Pujan Srivastava
You can use the following code to get info.
您可以使用以下代码获取信息。
VCard card = new VCard();
card.load(connection, "user@fqdn");
System.out.println("Voice: "+card.getPhoneHome("VOICE"));
回答by Joe Hildebrand
Try setting a vCard for that user with another client first, and see how that changes your results. In order to diagnose further, you'll need to turn on protocol debugging in Smack (use "-Dsmack.debugEnabled=true" on a desktop machine), and post the relevant bits here.
首先尝试使用另一个客户端为该用户设置 vCard,看看这会如何改变您的结果。为了进一步诊断,您需要在 Smack 中打开协议调试(-Dsmack.debugEnabled=true在台式机上使用“ ”),并在此处发布相关位。

