如何通过Facebook SDK发布Feed

时间:2014-03-11 03:12:57

标签: android facebook

我是Facebook SDK的新手,我正在尝试将文字发布到Facebook。

根据Facebook Graph API,我这样做了:

public void postmessage(View v)
{
  String m="test";

Session session = Session.getActiveSession();
 Log.i("log",session.getAccessToken());


  HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("https://graph.facebook.com/100002172912180/feed?message="+m+"&access_token="+session.getAccessToken());

    try {

        // Execute HTTP Post Request
        Log.i("log","send");
        HttpResponse response = httpclient.execute(httppost);
        Log.i("log",response.toString()+"end");
    } catch (Exception e) {
        // TODO Auto-generated catch block
    } 

}

令牌是对的,因为我可以从中得到正确的信息。 但是消息无法发布。 我没有收到任何错误消息,相反,根本没有消息。 我无法弄清楚它有什么问题,任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:0)

感谢GhostDerfel,打印异常时很容易解决。 http帖子需要在一个线程中运行。所以这是解决方案:

new Thread() {
            @Override
            public void run() {
                Session session = Session.getActiveSession();
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("https://graph.facebook.com/100002172912180/feed?message=test&access_token="+session.getAccessToken());

                HttpResponse response;
                try {
                    response = httpclient.execute(httppost);
                      Log.i("tag",EntityUtils.toString(response.getEntity()));
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
              ;
            }
        }.start();    

希望这会帮助那些坚持同样问题的人。 :)