处理Android Facebook API的响应对象

Chr*_*ock 6 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 );
Run Code Online (Sandbox Code Playgroud)

错误消息说:

org.json.JSONException: Unterminated object at character 25 of {Response:  responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"data":[{"pic_square":.....
Run Code Online (Sandbox Code Playgroud)

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

GraphObject go = response.getGraphObject();
Run Code Online (Sandbox Code Playgroud)

..我怎么能用这个获得GraphUser-Objects?

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

非常感谢你提前!

问候克里斯托弗

Chr*_*ock 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

问候

克里斯托弗

UPDATE

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

JSONObject  jso = response.getJSONObject();
JSONArray   arr = jso.getJSONArray("data");
Run Code Online (Sandbox Code Playgroud)