6

当前的 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 文档中处理响应对象的记录很差,我无法在网络上收到任何关于此的信息 :(

非常感谢您!

问候克里斯托弗

4

1 回答 1

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");
于 2012-12-13T16:24:31.210 回答