我正在创建一个与 facebook 集成的 Android 游戏,以使用图表检索他们的照片、名字和其他信息,我已经找到了如何使用下面的代码检索他们的照片,该代码运行良好,但我正在努力寻找提取其他信息(如名字等)的工作示例...
我用来检索照片并将其显示在 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();
}
}
我已经搜索了很多,但似乎无法找到有关如何使用图表检索其他信息的信息,其他信息意味着名字等......包括在Facebook开发者网站上(https://developers. facebook.com/docs/reference/api/)但找不到有效的示例。
任何人都可以向我展示一个工作示例来检索用户的名字,以便我可以将其显示在文本视图中吗?
使用这个网址
"https://graph.facebook.com/me/friends?access_token="+accessToken
你可以让你的朋友包含他们的名字和ID来获取他们中任何一个人的信息只需使用`"https://graph.facebook.com/"+yourFriendId+"?access_token="+accessToken'
这将返回您可以解析和使用的 jsonHttpClient 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();
}
如果您将请求 URL 更改为
/me
(即 https://graph.facebook.com/me?type=normal&method=GET&access_token=...
),您将获得当前经过身份验证的用户的所有可用个人资料信息。
此 JSON 对象应具有
first_name
和 last_name
,以及此处找到的其他属性。
查看这个方便的 API 浏览器工具,尝试一下您应该看到的内容。