I’ve been working on a simple app for android – and i’m trying to login using Single Sign-On using the Facebook Android SDK.
I’m trying to save the profile picture and username. I’m not quite sure what’s wrong – but nothing seems to happen after I Sign In on Facebook.
Here’s my code:
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);
}
What am I doing wrong?
Change this
URL image_value= new URL(“http://graph.facebook.com/” + userName + “/pictures” );
to this
URL image_value= new URL(“http://graph.facebook.com/” + userName + “/picture” );