解析 Facebook 的 XML 查询响应问题

发布于 2024-11-09 08:23:36 字数 1852 浏览 0 评论 0原文

嘿伙计们! :) 我正在开发一项服务,可以从 Facebook API 捕获点赞数、分享数等。 但有一个问题,因为我从 fqlResponse.getChild() 得到一个空指针异常。

我认为自动检测xmls doctype有问题,所以我手动定义了doctype,但问题仍然存在。也许 facebooks 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

感谢您的帮助! 怀特内克斯

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=\"" + 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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幻梦 2024-11-16 08:23:36

您可以执行以下操作:-

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

确保使用 %20 和 %22 适当地对 URL 中的空格和双引号进行编码。我已经测试了这段代码,它对我有用。

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.

生活了然无味 2024-11-16 08:23:36

它对我有用:

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

    }

当您调用 getChild 和 %20 & 时,我刚刚插入了“root.getNamespace()”查询中的 %22

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文