2012-06-06 187 views
0

我一直在爲Android的一個簡單的應用程序 - 我試圖登錄使用單一登錄使用Facebook的Android SDK。保存Facebook的個人資料圖片和用戶名Android

我試圖保存個人資料圖片和用戶名。我不太確定有什麼不對 - 但在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

改變這個 URL image_value = new URL(「http://graph.facebook.com/」+ userName +「/ pictures」); 至此 網址image_value =新網址(「http://graph.facebook.com/」+ userName +「/ picture」);

+0

謝謝 - 這是問題! – Alin

+0

不客氣。 – Parker

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,有一個稱爲效用的類。設置LOGGING = true。 再給它一次,看看日誌。來自facebook的日誌可能會說你的應用哈希與Facebook應用不匹配。在日誌中複製哈希,轉到您的Facebook應用程序,選中android應用程序的複選框並將哈希粘貼到相應的字段中。

+0

我已經試過這個 - 但還是不行!此外,我的應用程序在Facebook上登錄,並且用戶名和圖片被回收(我已選中) - 唯一的問題是用戶界面在登錄後似乎不會更新! – Alin

相關問題