Gan*_*Ako 4 android facebook facebook-graph-api
我在我的Android应用程序中使用Facebook SDK.我需要获取用户的所有信息.到现在为止,我可以获得名称ID电子邮件生日和用户的个人资料照片.我需要得到的最后一个是用户的位置或当前位置.我在下面使用此代码.
if(fb.isSessionValid()){
button.setImageResource(R.drawable.logout_button);
JSONObject obj = null;
URL img = null;
try {
String jsonUser = fb.request("me");
obj = Util.parseJson(jsonUser);
String id = obj.optString("id");
String name = obj.optString("name");
String bday = obj.optString("birthday");
String address = obj.optString("location");
String email = obj.optString("email");
((TextView) findViewById(R.id.txt)).setText("Welcome! "+name);
((TextView) findViewById(R.id.txtbday)).setText("Birthday: "+bday);
((TextView) findViewById(R.id.txtaddress)).setText("Address: "+address);
((TextView) findViewById(R.id.txtemail)).setText("Email: "+email);
img = new URL("http://graph.facebook.com/"+id+"/picture?type=normal");
Bitmap bmp = BitmapFactory.decodeStream(img.openConnection().getInputStream());
pic.setImageBitmap(bmp);
} catch (FacebookError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
button.setImageResource(R.drawable.login_button);
}
}
Run Code Online (Sandbox Code Playgroud)
这是权限
fb.authorize(MainActivity.this,new String[] {"email", "user_location", "user_birthday","publish_stream"}, new DialogListener() {
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ""+e, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
public void onError(DialogError e) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
Editor editor = sp.edit();
editor.putString("access_token", fb.getAccessToken());
editor.putLong("access_expires", fb.getAccessExpires());
editor.commit();
updateButtonImage();
}
public void onCancel() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Cancel", Toast.LENGTH_SHORT).show();
}
});
}
Run Code Online (Sandbox Code Playgroud)
感谢谁能提供帮助.
Graph API Explorer工具是查看图API端点响应的好方法.只需user_location通过单击"获取访问令牌"并查看"/ me"的输出来请求正确的权限(例如).当我获得具有权限的访问令牌时,user_location我能够看到当前的
https://developers.facebook.com/tools/explorer/?method=GET&path=me
要获取当前位置,这是如何做到的:
String currentCity = obj.getJSONObject("location").getString("name");
Run Code Online (Sandbox Code Playgroud)
使用较新的SDK,您可以通过图表API对象传递以下内容来获取位置详细信息
graphObject.getInnerJSONObject().getJSONObject("location").getString("id"))
Run Code Online (Sandbox Code Playgroud)