2012-09-22 115 views
2

我想通過我的應用程序獲取所有朋友的家鄉位置和當前位置。我開始知道,爲了獲得位置,我必須設置權限並編寫FQL。我寫了,網址變成 My Graph API format
但是當我使用這個鏈接來獲取JSON我沒有得到值。我試着用空格替換%20,然後嘗試着,即使如此,這個功能也很有用。 請告訴我出了什麼問題。並告訴我應該使用哪個網址。從Facebook獲取朋友的位置

我的代碼來獲得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(); 
      } 
+0

編碼字符串的location_tagged項目,並使用它,你會得到響應,也有特殊字符並不手動把%20代替空間編碼它 – user

+0

該網址已經在編碼形式..但我不能得到的信息..我試圖使用編碼格式以及解碼格式的網址.. – Vaibs

+0

@Vaibs:你能在你運行的地方發佈代碼這個FQL查詢? –

回答

0

你也可以做到這一點簡單的直通圖形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= 

這將返回我(認證用戶)+友

+0

雅我做到了,但它專門來爲喜歡和所有..它不提及每個朋友的位置(當前,家鄉)..這是不同的形式,我在我的問題中提到的鏈接.. – Vaibs

+0

你是對的!家鄉類型信息是用戶配置文件的一部分。我在回答中給出的例子會讓你'用戶發起的位置項目(簽入,標籤,...)'。 – guleryuz

0

我試圖通過空間替代20%和嘗試,甚至,但同樣力的工作。

對某個值進行URL編碼大約只是替換空格。還有其他字符,它們也希望被它們的%xy表示法替代 - 例如逗號。

URL編碼,以正確的方式整個 FQL查詢,它會工作:

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