解析Facebook XML查询响应问题
|
大家好! :)
我正在开发一种服务,该服务可以从Facebook API中捕获喜欢,分享等的次数。
但是有一个问题,因为我从fqlResponse.getChild()得到了一个空指针异常。
在我看来,xmls doctype的自动检测存在问题,因此我手动定义了doctype,但是问题仍然存在。也许facebook的xml文档类型不正确?
这是捕获方法:
public void refreshLikesSharesClicksAndTotal() throws JDOMException, IOException {
URL fqlURL = new URL(\"https://api.facebook.com/method/fql.query?query=select like_count, total_count, share_count, click_count from link_stat where url=\\\"\" + this.url.toString() + \"\\\"\");
Document inputXML = new SAXBuilder().build(fqlURL);
DocType docType = new DocType(\"xml\", \"http://www.w3.org/2001/XMLSchema-instance\");
inputXML.setDocType(docType);
Element fqlResponse = inputXML.getRootElement().getChild(\"link_stat\");
Element likes = fqlResponse.getChild(\"like_count\");
logger.info(\"Likes: \" + likes.getText());
Element shares = fqlResponse.getChild(\"share_count\");
Element clicks = fqlResponse.getChild(\"click_count\");
Element total = fqlResponse.getChild(\"total_count\");
this.likes = Integer.parseInt(likes.getText());
this.shares = Integer.parseInt(shares.getText());
this.clicks = Integer.parseInt(clicks.getText());
this.total = Integer.parseInt(total.getText());
}
XML示例:
https://api.facebook.com/method/fql.query?query=select%20%20like_count、%20total_count、%20share_count、%20click_count%20from%20link_stat%20where%20url=%22http://heise.de% 22
德语问题描述:
http://www.java-forum.org/xml-co/118648-problem-beim-parsen-facebook-xml.html
感谢帮助!
whitenexx
没有找到相关结果
已邀请:
2 个回复
弦砂牧扁
当您在查询中调用getChild和%20&%22时,我只是插入了\“ root.getNamespace()\”
担每棱绰
确保在URL中使用%20和%22适当地编码空格和双引号。我已经测试了此代码,并且对我有用。