2012-06-19 11 views
1

我正在創建一個與Facebook集成的安卓遊戲,以檢索他們的照片和名字以及使用圖形的其他信息,我已經發現如何使用下面的代碼檢索他們的照片它完美的作品,但我很努力地找到工作的例子,提取像first_name等其他信息...正在檢索android原生應用的facebook圖數據

我用來檢索照片並在ImageView中顯示它的代碼如下。

public void showPhoto(View v) { 
    try { 
     ImageView MyProfilePicImageView = (ImageView)findViewById(R.id.temp); 
     URL MyProfilePicURL = new URL("https://graph.facebook.com/me/picture?type=normal&method=GET&access_token="+ access_token); 
     Bitmap MyprofPicBitMap = null; 

     try { 
      MyprofPicBitMap = BitmapFactory.decodeStream(MyProfilePicURL.openConnection().getInputStream()); 
      MyProfilePicImageView.setImageBitmap(MyprofPicBitMap); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 
} 

我已經搜查高和低,但似乎無法找到如何使用圖表,其他信息意味着檢索其他信息FIRST_NAME等等......這裏包括了Facebook開發者網站上(https://developers.facebook.com/docs/reference/api/ )但找不到一個工作示例。

任何人都可以告訴我一個工作示例來檢索用戶的first_name,以便我可以在textview中顯示它嗎?

回答

4

使用此網址"https://graph.facebook.com/me/friends?access_token="+accessToken你可以得到你的朋友,其中包含他們的名字和ids來獲取他們中的任何一個的信息只需使用`「https://graph.facebook.com/"+yourFriendId+"?access_token="+accessToken 「 這將返回JSON可以解析和使用

HttpClient httpclient = new DefaultHttpClient(); 
String url = "https://graph.facebook.com/me/friends?access_token=" + URLEncoder.encode(token); 
HttpGet httppost = new HttpGet(url); 
try { 
       HttpResponse response = httpclient.execute(httppost); 

       // to get the response string 
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(
           response.getEntity().getContent(), "UTF-8")); 
       // used to construct the string 
       String res = ""; 
       for (String line = null; (line = reader.readLine()) != null;) { 
        res += line + "\n"; 

       } 
// here we will parse the response 
       JSONObject obj = new JSONObject(new JSONTokener(res)); 
       JSONArray data = obj.getJSONArray("data"); 
int len = data.length(); 
for (int i = 0; i < len; i++) { 
        JSONObject currentResult = data.getJSONObject(i); 
        String name = currentResult.getString("name"); 
        String icon = currentResult.getString("id"); 

        // do what ever you want 

       } 
} catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
+1

謝謝,這就是我正在尋找的:-) – TRicks

+0

我想要生日 – Prasad

+0

你可以指定你想要得到的字段嗎?fields = birthday,name這將返回生日,如果它可用可以使用圖形API資源管理器通過從左側區域指定所需的字段來查找正確的URL使用https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me%2Ffriends%3Ffields%3Dbirthday% 2Cname&版本= V2.2& – Amal

0

如果你改變你的請求URL只是/me(即https://graph.facebook.com/me?type=normal&method=GET&access_token=...),你會得到過驗證的用戶的所有可用配置文件信息。

此JSON對象應具有first_namelast_name,以及其他屬性here

看看這個handy API explorer tool玩弄你應該看到的。