如何在android中读取fql响应

时间:2013-01-20 08:04:12

标签: android facebook facebook-graph-api

我一直在做以下事情: https://developers.facebook.com/docs/howtos/androidsdk/3.0/run-fql-queries/#step2

具有以下代码(为简单起见,我更改了fql查询):

queryButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String fqlQuery = "SELECT name FROM user WHERE uid IN " +
                "(SELECT uid2 FROM friend WHERE uid1 = me()LIMIT 2)";
        Bundle params = new Bundle();
        params.putString("q", fqlQuery);
        Session session = Session.getActiveSession();
        Request request = new Request(session,
            "/fql",                         
            params,                         
            HttpMethod.GET,                 
            new Request.Callback(){         
                public void onCompleted(Response response) {
                    Log.i(TAG, "Result: " + response.toString());
                }                  
        }); 
        Request.executeBatchAsync(request);                 
    }
});

专门查看Log.i,response.toString()以这种格式返回一些内容:

Result: {Response:  responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"data":[{"name":"John Doe"},{"name":"Jake Josh"}]}}, error: null, isFromCache:false}

现在我的假设是响应是一个json obj,所以我尝试了这样的事情:

JSONObject json=new JSONObject(response);

但这不起作用,所以我不确定如何阅读我得到的回复,严格来说我只想循环遍历名称,但无法弄清楚如何实际阅读回复

2 个答案:

答案 0 :(得分:2)

试试这个:

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 name   = json_obj.getString( "name" );

            //...

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

拨打这样的电话:

public void onCompleted(Response response) {
     Log.i(TAG, "Result: " + response.toString());
     parseUserFromFQLResponse(response);
} 

答案 1 :(得分:1)

来自source for GraphObject 对于Response,看起来你可以做到:

JSONObject data = response.getGraphObject().getInnerJSONObject();