【问题标题】:Get Post Comments Using HttpsUrlConnection使用 HttpsUrlConnection 获取帖子评论
【发布时间】:2015-10-09 19:05:23
【问题描述】:

我想使用 HttpsUrlConnection 获取 Facebook 帖子的 cmets 这是网址https://graph.facebook.com/399455050251075/comments?access_token={my_access_token}

String urlAddress = "https://graph.facebook.com/" + postId + "/comments?access_token=" + "{my_access_token}";
        try {
            URL url = new URL(urlAddress);
            HttpsURLConnection httpURLConnection = (HttpsURLConnection) url
                    .openConnection();
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setRequestMethod("GET");
            String response = ConnectionController.getResponse(httpURLConnection);
            Log.v(Constants.TAG, urlAddress);
            Log.v(Constants.TAG, response);

        } catch (IOException e) {
            e.printStackTrace();
        }

它在浏览器上运行良好,但在 Android 中我得到 FileNotFoundException

注意:我不想使用 Facebook SDK 或任何其他库

【问题讨论】:

  • 找不到哪个文件?这似乎是您的构建而不是 API 的问题
  • 不是构建问题,见stackoverflow.com/questions/9365829/…
  • 您确认postId 的值正确吗?而且您的访问令牌周围实际上并没有花括号,对吗?如果您将完整的 URL 作为静态值写入代码中,而不将变量放入其中,您会得到相同的结果吗?
  • 是的,我在浏览器相同的 url 中尝试过,它可以工作

标签: android facebook facebook-graph-api


【解决方案1】:

从您的代码中删除 setDoOutput(true) 调用。这仅在您执行 POST 请求时才需要,并且将其用于您的 GET 请求将导致FileNotFoundException。

【讨论】:

  • 您应该致电httpURLConnection.getResponseCode() 以查看服务器返回的错误。另外,请确保您调用的服务器可以处理 Https 而不仅仅是 Http。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-16
  • 1970-01-01
相关资源
最近更新 更多