android - 登录后在另一个 Activity 中获取 Facebook 个人资料

标签 android facebook facebook-graph-api

大家好,我在 youtube 上发现了有趣的 Facebook 实现方法,虽然有些方法已被弃用,但我发现它很容易使用,因为我是我的 Android 应用程序中 Facebook 实现的新手。我遇到了一些问题,我通过登录 Activity 成功登录到 Facebook,但在那里他们在同一 Activity 中显示用户个人资料,在我的情况下,我不想在我保存用户的另一个 Activity 中显示他们共享首选项中的凭据和 session 有效但是在实现用户配置文件的解析内容时我什么也没得到不知道为什么这是我的代码。

在此输入验证码

private void updateView() 
{
    // TODO Auto-generated method stub
    if(fb.isSessionValid())
    {
        Intent openActivity=new Intent(FacebookLoginActivity.this,AccountInfoActivity.class);
        startActivity(openActivity);
    }
}

这里的 session 有效,所以我可以进行下一个 Activity

在此输入验证码

 public class AccountInfoActivity extends Activity
{
Facebook fb;
String id,name,email;
Bitmap bmp;
ImageView profile_pic;
TextView facebook_name,facebook_email;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_account_profile);

    String APP_ID= "**************";
    fb= new Facebook(APP_ID);

    profile_pic=(ImageView)findViewById(R.id.profile_pic);
    facebook_name=(TextView)findViewById(R.id.facebookName);
    facebook_email=(TextView)findViewById(R.id.facebookEmail);

     (new DataProgressTask()).execute();

}
 public class DataProgressTask extends AsyncTask<String, Void, Boolean> 
{
    ProgressDialog progress;

    public DataProgressTask() 
    {
        progress = new ProgressDialog(AccountInfoActivity.this);
    }
    protected void onPreExecute() 
    {
            progress.setMessage("Loading data ...");
            progress.show();
    }
    @Override
    protected void onCancelled() 
   {
        if ( progress != null )
        {
            progress.dismiss();
        }
    }

    protected void onPostExecute(final Boolean success) 
    {
        //Check if dialog still runing dismiss  
        if ( progress != null)
        {
            progress.dismiss();  
        }

        if (success)
        {
            facebook_name.setText(name);
            facebook_email.setText(email);
            profile_pic.setImageBitmap(bmp);
        }
        else
        {
            Toast.makeText(getBaseContext(), "Check your Internet Connection", Toast.LENGTH_SHORT).show();
        }


    }
    @SuppressWarnings("deprecation")
    protected Boolean doInBackground(final String... args) 
    {
        if(fb.isSessionValid())
        {

            JSONObject obj= null;
            URL img_url = null;

            String jsonUser;

            try 
            {


                jsonUser = fb.request("me");
                obj = Util.parseJson(jsonUser);

                id = obj.optString("id");
                name= obj.optString("name");
                email= obj.optString("email");
                //getting the image of the user
                img_url = new URL("http://graph.facebook.com/"+id+"/picture?type=small");
                //decode it to bitmap
                bmp =BitmapFactory.decodeStream(img_url.openConnection().getInputStream());
                profile_pic.setImageBitmap(bmp);

            } 
            catch (MalformedURLException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            catch (IOException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            catch (FacebookError e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            catch (JSONException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return true;
    }

   }}

如有任何帮助,我们将不胜感激。 谢谢

最佳答案

我设法通过将用户配置文件的数据从登录 Activity 发送到其他 Activity 来解决我的问题,如果 session 有效,则所有这些都已完成:D

关于android - 登录后在另一个 Activity 中获取 Facebook 个人资料,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24875781/

相关文章:

facebook - 如何使用 JavaScript SDK 中的 Graph API 获取 Facebook 用户的当前位置?

android:InstallAppProgress:安装错误代码:-15

jquery - Facebook 风格的图片库

PHP:Facebook/Twitter 集成

xcode - 如何在 iOS 6 中使用 ACAccountStore 类在 Objective-C 中发布到 Facebook

ios - ShareKit - 在 Facebook 上分享

php - JSONArray 从 android 到 Array php

android - 用声音软件看

android - 如何避免在 Adapter 类中多次调用 findViewById()?

ios - 移动应用好友邀请/通知