的Android的Facebook得到的所有资料信息
如何,我可以从Facebook获取所有的用户配置文件信息(如名字,姓氏,电子邮件等)的Android的Facebook得到的所有资料信息
我已经下载了FB SDK但对于获取的个人资料没有例子信息。
首先创建应用程序,并得到应用ID,然后在这个ID传给你这样的代码。 初始化全球
public static final String mAPP_ID = "Your APP ID";
Facebook mFacebook= new Facebook(mAPP_ID);
,并在你的按钮setOnClickListener的Create()
// facebook login button click event
try{
//mFacebook.logout(LoginActivity.this);
((Button)findViewById(R.id.loginPageFaceBookButton)).setOnClickListener(loginButtonListener);
SessionStore.restore(mFacebook,LoginPage.this);
} catch (Exception e) {
Toast.makeText(LoginPage.this,"Exception"+e.toString(), Toast.LENGTH_SHORT).show();
}
// loginButtonListener
//----------------------------------------------
private OnClickListener loginButtonListener = new OnClickListener()
{
public void onClick(View v)
{
if(!mFacebook.isSessionValid())
{
mFacebook.authorize(LoginPage.this, new String[] {"publish_stream","email","user_groups","read_stream","user_about_me","offline_access"},Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}
else
{
try
{
JSONObject json = Util.parseJson(mFacebook.request("me"));
facebookID = json.getString("id");
facebookEmail = json.getString("email");
faceBooklastName=json.getString("last_name");
faceBookFirstName=json.getString("first_name");
}
catch (Exception e)
{
// TODO: handle exception
//Toast.makeText(LoginActivity.this,"Exception FB "+e.toString(), Toast.LENGTH_SHORT).show();
}
catch(FacebookError error)
{
Toast.makeText(LoginPage.this,error.toString(), Toast.LENGTH_SHORT).show();
}
}
}
};
//onActivityResult
//***********************************************************
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
// DialogListener CLASS STATRT HERE.
public final class LoginDialogListener implements DialogListener
{
public void onComplete(Bundle values)
{
try
{
JSONObject json = Util.parseJson(mFacebook.request("me"));
facebookID = json.getString("id");
facebookEmail = json.getString("email");
SessionStore.save(mFacebook, LoginPage.this); Toast.makeText(LoginPage.this,"facebookID :"+facebookID+" \n "+"facebookEmail : "+facebookEmail, Toast.LENGTH_SHORT).show();
}
catch(Exception error)
{
Toast.makeText(LoginPage.this, error.toString(), Toast.LENGTH_SHORT).show();
}
catch(FacebookError error)
{
Toast.makeText(LoginPage.this, error.toString(), Toast.LENGTH_SHORT).show();
}
}
public void onFacebookError(FacebookError error) {
Toast.makeText(LoginPage.this, "Something went wrong. Please try again.1"+error.toString(), Toast.LENGTH_LONG).show();
}
public void onError(DialogError error) {
Toast.makeText(LoginPage.this, "Something went wrong. Please try again.2"+error.toString(), Toast.LENGTH_LONG).show();
}
public void onCancel() {
Toast.makeText(LoginPage.this, "Something went wrong. Please try again.3", Toast.LENGTH_LONG).show();
}
/****** Facebook Login End *******/
}
谢谢。这很有用 – 2012-10-16 11:24:17
Facebook mFacebook =新的Facebook(mAPP_ID)现在已被弃用,是否有任何其他方式做Facebook的Android应用程序集成。 – keen 2013-07-08 07:07:44
这里是我
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Toast;
import com.facebook.Request;
import com.facebook.Request.GraphUserCallback;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.Session.StatusCallback;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.x.y.android.R;
public class FBConnect extends FragmentActivity {
private static final String TAG = "FacebookConnect";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook_connect);
if (Session.getActiveSession() == null
|| Session.getActiveSession().isClosed()) {
Session.openActiveSession(this, true, new StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
System.out.println("State= " + state);
if (session.isOpened()) {
System.out.println("Token=" + session.getAccessToken());
Request.executeMeRequestAsync(session,
new GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
System.out.println("User=" + user);
}
if (response != null) {
System.out.println("Response="
+ response);
Toast.makeText(FBConnect.this,
response.toString(),
Toast.LENGTH_LONG).show();
}
}
});
}
if (exception != null) {
System.out.println("Some thing bad happened!");
exception.printStackTrace();
}
}
});
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}
}
谢谢Thamme Gowada – 2013-05-14 06:20:27
// Make an API call to get user data and define a
// new callback to handle the response.
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
// If the response is successful
if (session == Session.getActiveSession()) {
if (user != null) {
// Set the id for the ProfilePictureView
// view that in turn displays the profile picture.
profilePictureView.setProfileId(user.getId());
// Set the Textview's text to the user's name.
userNameView.setText(user.getName());
}
}
if (response.getError() != null) {
// Handle errors, will do so later.
}
}
});
request.executeAsync();
工作了最快的方式这只是得到用户数据!您可以根据链接到FB文档的第一个答案找到所有相应的详细信息,其中包含所有可以检索的相关字段!
某些信息如电子邮件地址需要向认证请求添加特定的权限标志。请参阅http://stackoverflow.com/questions/3611682/facebook-graph-api-how-to-get-users-email – larham1 2012-02-14 23:08:43