Facebook新sdk - 无法获得好友列表

时间:2011-11-18 11:01:25

标签: android facebook facebook-fql facebook-android-sdk

我正在使用新的Facebook-sdk并使用hackbook示例来获取使用FriendsList.java文件的朋友列表。 Friendlist Activity每次都被杀死,我在logcat中遇到这个错误:

11-18 16:20:51.141: ERROR/JavaBinder(7881): !!! FAILED BINDER TRANSACTION !!!.

每次都可以重现这个问题。注意配置文件中的朋友数量为4500.

11-18 16:19:27.932: DEBUG/Facebook-Util(7881): GET URL: https://api.facebook.com/restserver.php?access_token=**********&query=select+name%2C+current_location%2C+uid%2C+pic_square+from+user+where+uid+in+%28select+uid2+from+friend+where+uid1%3Dme%28%29%29+order+by+name&method=fql.query&format=json

11-18 16:20:50.982: WARN/InputManagerService(3426): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4665a200
11-18 16:20:51.141: ERROR/JavaBinder(7881): !!! FAILED BINDER TRANSACTION !!!

请帮忙!

3 个答案:

答案 0 :(得分:2)

我没有发布任何代码,因为我在Facebook - Hackbook example for Android.使用了Facebook sdk的示例代码 最后我找到了问题并解决了它。

首先问题不在facebook sdk中,而在于响应的大小。如果您在Facebook中有超过4k的朋友,并且使用 fql.query 方法查询好友列表,则Json响应大小超过500kb。现在,如果您尝试通过IPC(意图额外数据)将此500kb响应传递给另一个活动,则该过程失败,原因为 !!活页夹交易失败!! 有关失败的活页夹交易的更多信息可以在Failed binder transaction discussion

找到

我是如何解决这个问题的: 我使用了与上面发布的链接相同的过程。即将响应写入文件并将文件链接发送到其他活动而不是完整的500kb响应。

答案 1 :(得分:2)

以下代码对我来说更好。 此代码提供好友列表。

public JSONObject getFrienList(String token)
{
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;
    try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httppost = new HttpGet("https://graph.facebook.com/me/friends?      access_token=" + token);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("ERROR", "Error in http connection "+e.toString());
    }
    //convert response to string
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) 
        {
            sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
        Log.i("RESULT",result);
    }
    catch(Exception e)
    {
            Log.e("ERROR", "Error converting result "+e.toString());
    }

    //try parse the string to a JSON object
    try
    {
        jArray = new JSONObject(result);
        Log.i("RESULT","JSON ARRAY"+jArray.toString());
    }
    catch(JSONException e)
    {
            Log.e("ERROR", "Error parsing data "+e.toString());
    }

    return jArray;  
}

答案 2 :(得分:1)

你现在在做什么我不知道......因为你没有发布任何代码..但这就是我得到朋友列表的方式。

加载好友列表信息。

HttpGet httpget = new HttpGet("https://graph.facebook.com/me/friends?access_token=" + mAccessToken); 
        HttpResponse response;
        try
        {
             response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();
                if (entity != null)
                {
                    result = EntityUtils.toString(entity);
                    Log.i("Request String",result);
                    parseJSON(result);
                }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

解析朋友的详细信息

 private void parseJSON(String data1)
        {
        try{
                JSONObject jObj = new JSONObject(data1);if(jObj==null) Log.i("-----------","JOBJ IS NULL");
                JSONArray jObjArr= jObj.optJSONArray("data");if(jObjArr==null) Log.i("-----------","JOBJARR IS NULL");
                    int lon = jObjArr.length();
                    String  frnd_data[][] = new String[jObjArr.length()][2];
                    entity1 = new String[jObjArr.length()];
                    Log.i("Frineds >>", "" + lon);
                    for(int i = 0 ;i< lon; i++)
                        {
                          JSONObject tmp = jObjArr.optJSONObject(i);
                          frnd_data[i][0]=tmp.getString("name");
                          frnd_data[i][1]=tmp.getString("id");
                          Log.i("Name", frnd_data[i][0]);
                          Log.i("Name AND Id" , frnd_data[i][0] +"  "+ frnd_data[i][1]);
                          entity1[i] = getFriend("https://graph.facebook.com/"+frnd_data[i][1]+"?access_token="+mAccessToken);
                        }
            }
            catch (Exception e)
            {
                Log.i("Exception1 >> ", e.toString());
            }
        }
      public String getFriend(String tmpurl) 
      {
          HttpClient httpclient = new DefaultHttpClient();
          String temp_result = null;
            HttpGet httpget = new HttpGet(tmpurl); 
            HttpResponse response;
            try
            {
                 response = httpclient.execute(httpget);
                    HttpEntity entity = response.getEntity();
                    if (entity != null)
                    {
                        temp_result = EntityUtils.toString(entity);
                        Log.i("Request String",temp_result);
                        return temp_result;
                    }
            }
           catch (Exception e) 
           {
             e.printStackTrace();
           }
           return temp_result;
      }