Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Android Facebook API's Response Object

The current Facebook API v3.0.2.b will return a com.facebook.Response object that holds the response. Do you know how to parse this? The following code will raise an Exception :(

//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 );

The error message says:

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

Does anybody know what's the correct solution? Shall I use

GraphObject go = response.getGraphObject();

.. how can I get GraphUser-Objects with that?

Sorry, this seems like a trivial issue but processing the Response-object is poorly documented in the facebook docs and I wasn't able to receive anything about this on the web :(

Thank you very much in advance!

Greetings Christopher

like image 855
Christopher Stock Avatar asked Dec 13 '12 08:12

Christopher Stock


People also ask

How does an API work with Facebook?

The Facebook Graph API is an HTTP-based API that allows developers to extract data and functionality from the Facebook platform. Applications can use this API to programmatically query data, post in pages and groups, and manage ads, among other tasks. The other Facebook APIs are extensions of the Graph API.

Does Facebook allow API?

The Graph API is the primary way to get data into and out of the Facebook platform. It's an HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.

What data can I get from Facebook API?

With the Facebook Graph API, you can access a wide range of user data, including user information such as name, email address, birthday, and friends list in JSON. You can also access information about pages that a user has liked or engaged with on Facebook, as well as events and photos that they have posted or shared.


1 Answers

this is the solution that worked for me - I had to investigate the response and play around a bit with some methods but finally solved it :)

/************************************************************************
*   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();
    }
}

Hope this helps anybody someday.

Greetings

Christopher

UPDATE

GraphObject is no longer a class, so just:

JSONObject  jso = response.getJSONObject();
JSONArray   arr = jso.getJSONArray("data");
like image 139
Christopher Stock Avatar answered Oct 17 '22 13:10

Christopher Stock