处理Android Facebook API的响应对象

时间:2012-12-13 08:22:22

标签: android facebook facebook-graph-api

当前的Facebook API v3.0.2.b将返回一个保存响应的com.facebook.Response对象。你知道如何解析这个吗?以下代码将引发异常:(

//execute the request
Request request = new Request
(
    session,
    "/fql",
    params,
    HttpMethod.GET,
    new Request.Callback()
    {
        @Override
        public void onCompleted( Response response )
        {
            try
            {
                JSONArray json = new JSONArray( response.toString() );
            }
            catch ( Throwable t )
            {
                System.err.println( t );
            }
        }
    }
);
Request.executeBatchAsync( request );

错误消息显示:

org.json.JSONException: Unterminated object at character 25 of {Response:  responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"data":[{"pic_square":.....

有人知道什么是正确的解决方案吗?我要用

GraphObject go = response.getGraphObject();

..我怎样才能获得GraphUser-Objects?

很抱歉,这似乎是一个微不足道的问题,但在facebook文档中处理Response-object的文档很少,我无法在网上收到任何有关此内容的信息:(

非常感谢你!

问候克里斯托弗

1 个答案:

答案 0 :(得分:31)

这是适合我的解决方案 - 我不得不调查响应并用一些方法玩一下但最终解决了它:)

/************************************************************************
*   Parses the user data from the facebook FQL
*
*   "SELECT uid, name, pic_square FROM user WHERE uid IN ( SELECT uid2 FROM friend WHERE uid1 = me() )"
*
*   This is the example query from
*   {@link http://developers.facebook.com/docs/howtos/androidsdk/3.0/run-fql-queries/}
*
*   @param  response    The facebook response from the FQL
************************************************************************/
public static final void parseUserFromFQLResponse( Response response )
{
    try
    {
        GraphObject go  = response.getGraphObject();
        JSONObject  jso = go.getInnerJSONObject();
        JSONArray   arr = jso.getJSONArray( "data" );

        for ( int i = 0; i < ( arr.length() ); i++ )
        {
            JSONObject json_obj = arr.getJSONObject( i );

            String id     = json_obj.getString( "uid"           );
            String name   = json_obj.getString( "name"          );
            String urlImg = json_obj.getString( "pic_square"    );

            //...

        }
    }
    catch ( Throwable t )
    {
        t.printStackTrace();
    }
}

希望有一天能帮助任何人。

问候

克里斯托弗

<强>更新

GraphObject不再是一个类,所以只需:

JSONObject  jso = response.getJSONObject();
JSONArray   arr = jso.getJSONArray("data");