Hey guys! :) I'm working on a service wich catches the number of likes, shares and so on from the Facebook API. But there is a problem because i get a nullpointer exception from fqlResponse.getChild().
In my opinion there is a problem with the automatic detection of the xmls doctype, so i defined the doctype manually, but the problem still exists. Maybe the facebooks xml doctype isn't correct? Here is the catching method:
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=\"" + thi开发者_如何学Pythons.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 Example: 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
German problem description: http://www.java-forum.org/xml-co/118648-problem-beim-parsen-facebook-xml.html
Thanks for help! whitenexx
You can do something like this:-
URL url = new URL("https://api.facebook.com/method/fql.query?query=select%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20url=%22http://heise.de%22");
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(url.openStream());
XPath xPath = XPathFactory.newInstance().newXPath();
int likeCount = Integer.parseInt(xPath.evaluate("//like_count/text()", doc));
int totalCount = Integer.parseInt(xPath.evaluate("//total_count/text()", doc));
int shareCount = Integer.parseInt(xPath.evaluate("//share_count/text()", doc));
int clickCount = Integer.parseInt(xPath.evaluate("//click_count/text()", doc));
Make sure you encode the spaces and double quotes in the URL with %20 and %22 appropriately. I have tested this code, and it works for me.
It works for me:
public void refreshLikesSharesClicksAndTotal() throws JDOMException, IOException {
URL fqlURL = new URL("https://api.facebook.com/method/fql.query?query=select%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20url=%22http://heise.de%22");
URLConnection openConnection = fqlURL.openConnection();
String contentType = openConnection.getContentType();
Document inputXML = new SAXBuilder().build(fqlURL);
DocType docType = new DocType("xml", "http://www.w3.org/2001/XMLSchema-instance");
inputXML.setDocType(docType);
Element root = inputXML.getRootElement();
Element fqlResponse = root.getChild("link_stat", root.getNamespace());
Element likes = fqlResponse.getChild("like_count", root.getNamespace());
Element shares = fqlResponse.getChild("share_count", root.getNamespace());
Element clicks = fqlResponse.getChild("click_count", root.getNamespace());
Element total = fqlResponse.getChild("total_count", root.getNamespace());
int alikes = Integer.parseInt(likes.getText());
int ashares = Integer.parseInt(shares.getText());
int aclicks = Integer.parseInt(clicks.getText());
int atotal = Integer.parseInt(total.getText());
}
I just inserted "root.getNamespace()" when you call getChild and %20 & %22 in the query
精彩评论