从facebook获取朋友的位置

时间:2012-09-22 08:36:19

标签: android facebook facebook-graph-api

我想通过我的应用程序获取所有朋友的家乡位置和当前位置。我开始知道,为了获取位置,我必须设置权限并编写FQL。我写了那个,URL变成了 My Graph API format
但是当我使用这个链接来获取json时,我没有得到这些值。我尝试用空间替换%20并尝试了,但是这样做也很有效。 请告诉我出了什么问题。告诉我应该使用哪个URL。

获取json值的代码..:

String q = "https://developers.facebook.com/tools/explorer?fql=SELECT name, uid, current_location, hometown_location FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())";
              HttpClient httpClient = new DefaultHttpClient();
              HttpGet httpGet = new HttpGet(q);
              try {
                    HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();
                    if (httpEntity != null){
                        InputStream inputStream = httpEntity.getContent();
                        Reader in = new InputStreamReader(inputStream);
                        BufferedReader bufferedreader = new BufferedReader(in);
                        StringBuilder stringBuilder = new StringBuilder();

                        String stringReadLine = null;

                        while ((stringReadLine = bufferedreader.readLine()) != null) {
                         stringBuilder.append(stringReadLine + "\n");
                         }

                        qResult = stringBuilder.toString();
                        inputStream.close();
                       }
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
              try {
                 Log.e("in try block", "in try block");
                JSONObject JsonObject = new JSONObject(qResult);
                JSONArray JsonArray = JsonObject.getJSONArray("data");
                for(int i = 0;i < JsonArray.length(); i++){
                    Log.e("jksdhkvjsdlvm", "inside 1st for loop");
                    JSONObject fbInfo = JsonArray.getJSONObject(i);
                    name = fbInfo.getString("name");
                    uid = fbInfo.getString("uid");
                    current_location = JsonObject.getJSONArray("current_location");
                    for(int j = 0;j < current_location.length(); j++){
                        JSONObject currentInfo = current_location.getJSONObject(j);
                        c_city = currentInfo.getString("city");
                        c_state = currentInfo.getString("state");
                        c_country = currentInfo.getString("country");
                        Log.e("--->>>>", c_city);
                    }
                    hometown_location = JsonObject.getJSONArray("hometown_location");
                    for(int k = 0;k < hometown_location.length(); k++){
                        JSONObject homeInfo = hometown_location.getJSONObject(k);
                        h_city = homeInfo.getString("city");
                        h_state = homeInfo.getString("state");
                        h_country = homeInfo.getString("country");
                    }
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

2 个答案:

答案 0 :(得分:0)

  

我尝试用空格替换%20并尝试了,但是这样做也很有效。

对值进行URL编码大约只是替换空格。还有其他角色,也希望被他们的%xy表示替换 - 例如commata。

以正确的方式对整个 FQL查询进行URL编码,它将起作用:

https://developers.facebook.com/tools/explorer?fql=SELECT+name%2C+uid%2C+current_location%2C+hometown_location+FROM+user+WHERE+uid+in+%28SELECT+uid2+FROM+friend+WHERE+uid1+%3D+me%28%29%29

答案 1 :(得分:0)

你也可以通过Graph API更简单。

1)您需要具有权限的access_token;

To read Posts with location information, you need:
    user_photos or friends_photos for Photos
    user_status or friends_status for Posts (not including Photos)
    user_checkins or friends_checkins for Checkins only (excluding all other types of Posts)

2)然后调用此

https://graph.facebook.com/search?type=checkin&access_token=

这将返回Me(经过身份验证的用户)+好友

的location_tagged项目