保存Facebook个人资料图片和用户名Android

时间:2012-06-06 18:57:27

标签: android facebook

我一直在研究一个简单的Android应用程序 - 我正在尝试使用Facebook Android SDK使用单点登录进行登录。

我正在尝试保存个人资料图片和用户名。我不太确定出了什么问题 - 但在我登录Facebook后似乎没有任何事情发生。

这是我的代码:

public class fbLogin extends Activity {
 Facebook facebook = new Facebook("MY-APP-ID");
 private SharedPreferences mPrefs;
 public boolean flag=false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fblogin_screen);
        mPrefs=getPreferences(MODE_PRIVATE);
        String acces_token=mPrefs.getString("acces_token",null);
        long expires=mPrefs.getLong("acces_expires", 0);

        if(acces_token!=null)
        {
            facebook.setAccessToken(acces_token);
        }
        if(expires!=0)
        {
            facebook.setAccessExpires(expires);
        }

        if(!facebook.isSessionValid())
        {
        facebook.authorize(this, new DialogListener() {

            public void onComplete(Bundle values) {
                flag=true;
                 SharedPreferences.Editor editor = mPrefs.edit();
                 editor.putString("access_token", facebook.getAccessToken());
                 editor.putLong("access_expires", facebook.getAccessExpires());
                 editor.commit();

            }

            public void onFacebookError(FacebookError error) {}

            public void onError(DialogError e) {}

            public void onCancel() {}
        });
    } else{
        flag=true;
        }

        if(flag==true)
        {
            try {
                JSONObject me = new JSONObject(facebook.request("me"));
                String id=me.getString("id");
                String userName=me.getString("username");
                ImageView picture;
                TextView usr = (TextView)findViewById(R.id.userName);
                picture = (ImageView) findViewById(R.id.profilePicture);
                URL image_value= new URL("http://graph.facebook.com/" + userName + "/pictures" );
                Bitmap profPict=BitmapFactory.decodeStream(image_value.openConnection().getInputStream());
                picture.setImageBitmap(profPict);
                usr.setText(userName);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        facebook.authorizeCallback(requestCode, resultCode, data);
    }

我做错了什么?

2 个答案:

答案 0 :(得分:2)

改变这个 URL image_value =新网址(“http://graph.facebook.com/”+ userName +“/ pictures”); 对此 URL image_value =新网址(“http://graph.facebook.com/”+ userName +“/ picture”);

答案 1 :(得分:1)

把这个:

if(flag==true)
        {
            try {
                JSONObject me = new JSONObject(facebook.request("me"));
                String id=me.getString("id");
                String userName=me.getString("username");
                ImageView picture;
                TextView usr = (TextView)findViewById(R.id.userName);
                picture = (ImageView) findViewById(R.id.profilePicture);
                URL image_value= new URL("http://graph.facebook.com/" + userName + "/pictures" );
                Bitmap profPict=BitmapFactory.decodeStream(image_value.openConnection().getInputStream());
                picture.setImageBitmap(profPict);
                usr.setText(userName);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

在它自己的方法中调用getInfo(),然后在你的对话框监听器中调用getInfo()就像这样

facebook.authorize(this, new DialogListener() {

            public void onComplete(Bundle values) {
                flag=true;
                 SharedPreferences.Editor editor = mPrefs.edit();
                 editor.putString("access_token", facebook.getAccessToken());
                 editor.putLong("access_expires", facebook.getAccessExpires());
                 editor.commit();
                 //We're authorized so getInfo();
                 getInfo();
            }

            public void onFacebookError(FacebookError error) {}

            public void onError(DialogError e) {}

            public void onCancel() {}
        });

现在,在Facebook SDK中,有一个名为Utility的类。设置LOGGING = true。 再试一次,看看日志。来自facebook的日志可能会说你的应用程序哈希和facebook应用程序之间存在不匹配。复制日志中的哈希值,转到你的脸书应用程序,选中Android应用程序框并将哈希值粘贴到相应的字段中。