0

我一直在为 android 开发一个简单的应用程序 - 我正在尝试使用 Facebook Android SDK 使用 Single Sign-On 登录。

我正在尝试保存个人资料图片和用户名。我不太确定出了什么问题 - 但我在 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);
    }

我究竟做错了什么?

4

2 回答 2

2

更改此 URL image_value= new URL("http://graph.facebook.com/" + userName + "/pictures" ); 到这个 URL image_value= new URL("http://graph.facebook.com/" + userName + "/picture" );

于 2012-06-11T16:36:19.807 回答
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 应用程序之间存在不匹配。复制日志中的哈希值,转到您的 Facebook 应用程序,选中 android 应用程序框并将哈希值粘贴到相应的字段中。

于 2012-06-06T19:05:01.743 回答