我一直在为 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);
}
我究竟做错了什么?