1

我正在创建一个与 facebook 集成的 android 游戏,以使用图表检索他们的照片和名字以及其他信息,我已经找到了如何使用以下代码检索他们的照片,该代码运行良好,但我正在努力寻找工作提取其他信息(例如 first_name 等)的示例...

下面是我用来检索照片并将其显示在 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/)但找不到工作示例。

任何人都可以向我展示一个工作示例来检索用户的名字,以便我可以在文本视图中显示它吗?

4

2 回答 2

4

使用此 url "https://graph.facebook.com/me/friends?access_token="+accessToken,您可以获得包含他们的姓名和 ID 的朋友,以获取有关他们中的任何一个的信息,只需使用 `"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();
            }
于 2012-06-19T21:19:45.047 回答
0

如果您将请求 URL 更改为仅/me(ie https://graph.facebook.com/me?type=normal&method=GET&access_token=...),您将获得当前经过身份验证的用户的所有可用配置文件信息。

此 JSON 对象应具有first_name和,以及此处last_name发现的其他属性。

看看这个方便的 API 浏览器工具来玩转你应该看到的东西。

于 2012-06-19T21:21:27.013 回答