2011-10-08 62 views
6

這個代碼我如何在Facebook的FQL做孤單實料FQL在Java

https://developers.facebook.com/docs/reference/api/batch/ 


curl \ 
    -F 'access_token=…' \ 
    -F 'batch=[ \ 
      {"method": "GET", "relative_url": "me"}, \ 
      {"method": "GET", "relative_url": "me/friends?limit=50"} \ 
     ]'\ 
    https://graph.facebook.com 

它想用JSON 被髮送,但我真的不知道如何做到這一點 任何幫助嗎?

感謝

+0

你可以在你的問題更精確?你試過了什麼,你期望什麼,結果如何?你有沒有試過上面的命令?它失敗了嗎?你有捲曲安裝?它迴應了嗎?你得到了什麼?你看到了什麼錯誤信息? –

回答

3

你可以簡單的使用BatchFB API的功能非常強大和容易的,你不要有交易將所有這些東西,它使用的FQL 例如讓所有的朋友

Later<ArrayNode> friendsArrayList = this.Batcher.query("SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"); 
    for (JsonNode friend : friendsArrayList.get()) { 
      ....... 
     } 

和它的批次

2

我相信你的問題是如何使用Facebook的圖形API執行一批請求。對於這一點,你必須發出一個POST請求

"https://graph.facebook.com" 

和後要發送的數據應該是

"batch=[{'method': 'GET', 'relative_url': 'me'}, {'method': 'GET', 'relative_url': 'me/friends?limit=50'}]&[email protected]" 
你的情況

[@accesstoken必須與你的訪問令牌值替換。

該請求將返回訪問令牌所有者的詳細信息(通常是當前登錄的用戶)以及用戶的50個Facebook好友(包含ID和名稱字段)的列表以及頁眉(可省略)。

我不確定你的意思是java還是Javascript。請具體說明。

我基本上是C#程序員。將爲您提供一個代碼,以在C#中執行上述請求。

WebRequest webRequest = WebRequest.Create("https://graph.facebook.com"); 
webRequest.Method = "POST"; 
webRequest.ContentType = "application/x-www-form-UrlEncoded"; 
byte[] buffer = Encoding.UTF8.GetBytes("batch=[{'method': 'GET', 'relative_url': 'me'}, {'method': 'GET', 'relative_url': 'me/friends?limit=50'}]&[email protected]"); 
webRequest.ContentLength = buffer.Length; 
using (Stream stream = webRequest.GetRequestStream()) 
{ 
    stream.Write(buffer, 0, buffer.Length); 
    using (WebResponse webResponse = webRequest.GetResponse()) 
    { 
     if (webResponse != null) 
     { 
      using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) 
      { 
       string data = streamReader.ReadToEnd(); 
      } 
     } 
    } 
} 

這裏可變數據將包含結果。

1

Salah,這裏是我用作參考的例子,我很抱歉,雖然我不記得我在哪裏找到了。

FB.api("/", "POST", { 
    access_token:"MY_APPLICATION_ACCESS_TOKEN", 
    batch:[ 
     { 
      "method":"GET", 
      "name":"get-photos", 
      "omit_response_on_success": true, 
      "relative_url":"MY_ALBUM_ID/photos" 
     }, 
     { 
      "method": "GET", 
      "depends_on":"get-photos", 
      "relative_url":"{result=get-photos:$.data[0].id}/likes" 
     } 
    ] 
}, function(response) { 
    if (!response || response.error) { 
     console.log(response.error_description); 
    } else {  
     /* Iterate through each Response */ 
     for(var i=0,l=response.length; i<l; i++) { 
      /* If we have set 'omit_response_on_success' to true in the Request, the Response value will be null, so continue to the next iteration */ 
      if(response[i] === null) continue; 
      /* Else we are expecting a Response Body Object in JSON, so decode this */ 
      var responseBody = JSON.parse(response[i].body); 
      /* If the Response Body includes an Error Object, handle the Error */ 
      if(responseBody.error) { 
       // do something useful here 
       console.log(responseBody.error.message); 
      } 
      /* Else handle the data Object */ 
      else { 
       // do something useful here 
       console.log(responseBody.data); 
      } 
     } 
    } 
});