2011-05-23 48 views
1

嘿傢伙! :) 我正在從Facebook API獲取喜歡,分享等等的服務。 但有一個問題,因爲我從fqlResponse.getChild()獲得一個空指針異常。解析Facebook的XML查詢響應問題

在我看來,自動檢測xmls文檔類型存在問題,所以我手動定義了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

回答

0

它爲我的作品:

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()); 

    } 

我剛插入 「root.getNamespace()」 當你調用getChild和20%& 22%在查詢

+0

你能告訴我它是否適合您??謝謝,我已閱讀http://www.java-forum.org/xml-co/118648-problem-beim-parsen-facebook-xml.html上的討論,但我的解決方案似乎更像您的解決方案。 – Shilaghae 2011-05-25 10:14:16

1

你可以做這樣的事情: -

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)); 

確保您編碼在URL中的空格和雙引號%和20%22恰當。我已經測試了這個代碼,它適用於我。