2012-01-10 85 views
1

我想將facebook與android mobile整合,我需要做的就是從我的應用程序中取出一些文本並將其作爲Facebook上的狀態,我試過facebook_sdk作爲目的,當我看到調試模式,應用程序被終止在該行facebook.authorize(),雖然有在menifest完整的互聯網連接...下面是我的代碼: 在此先感謝Android中的Facebook授權問題

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    button1 = (Button)findViewById(R.id.Button1); 
    button2 = (Button)findViewById(R.id.Button2); 
    tv1 = (TextView)findViewById(R.id.TextView1); 
    et1 = (EditText)findViewById(R.id.EditText1); 
    button1.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      b1Click(); 
     } 

    }); 
    button2.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      b2Click(); 
     } 

    }); 
} 
private void saveFBToken(String token, long tokenExpires){ 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    prefs.edit().putString("FacebookToken", token).commit(); 
} 

private void fbAuthAndPost(final String message){ 

    facebook.authorize(this, new String[]{"publish_stream"},new DialogListener() { 

     @Override 
     public void onComplete(Bundle values) { 
      Log.d(this.getClass().getName(),"Facebook.authorize Complete: "); 
      saveFBToken(facebook.getAccessToken(), facebook.getAccessExpires()); 
      updateStatus(values.getString(Facebook.TOKEN), message); 
     } 

     @Override 
     public void onFacebookError(FacebookError error) { 
      Log.d(this.getClass().getName(),"Facebook.authorize Error: "+error.toString()); 
     } 

     @Override 
     public void onError(DialogError e) { 
      Log.d(this.getClass().getName(),"Facebook.authorize DialogError: "+e.toString()); 
     } 

     @Override 
     public void onCancel() { 
      Log.d(this.getClass().getName(),"Facebook authorization canceled"); 
     } 
    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode,Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode){ 
    case Facebook.FORCE_DIALOG_AUTH: 
     facebook.authorizeCallback(requestCode, resultCode, data); 
    } 
} 

private void b1Click(){ 

    mAuthAttempts = 0; 

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    mFacebookToken = prefs.getString("FacebookToken", ""); 

    if(mFacebookToken.equals("")){ 
     fbAuthAndPost(et1.getText().toString()); 
    }else{ 
     updateStatus(mFacebookToken,et1.getText().toString()); 
    } 
} 

private void b2Click(){ 
    mAuthAttempts = 0; 
    updateStatus(facebook.TOKEN,et1.getText().toString()); 
} 

public void updateStatus(String accessToken, String message){ 
    try {   
     Bundle bundle = new Bundle(); 
     bundle.putString("message", message);   
     bundle.putString(Facebook.TOKEN,accessToken);   
     String response = facebook.request("me/feed",bundle,"POST");   
     Log.d("UPDATE RESPONSE",""+response); 
     showToast("Update process complete. Respose:"+response); 
     if(response.indexOf("OAuthException") > -1){ 
      if(mAuthAttempts==0){ 
       mAuthAttempts++; 
       fbAuthAndPost(message); 
      }else{ 
       showToast("OAuthException:"); 
      } 
     } 
    } catch (MalformedURLException e) {   
     Log.e("MALFORMED URL",""+e.getMessage()); 
     showToast("MalformedURLException:"+e.getMessage()); 
    } catch (IOException e) {   
     Log.e("IOEX",""+e.getMessage()); 
     showToast("IOException:"+e.getMessage()); 
    } 

    String s = facebook.getAccessToken()+"\n"; 
    s += String.valueOf(facebook.getAccessExpires())+"\n"; 
    s += "Now:"+String.valueOf(System.currentTimeMillis())+"\n"; 
    tv1.setText(s); 
} 
private void showToast(String message){ 
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); 
} 
+0

把logcat ...將顯示錯誤的詳細信息。 – 2012-01-10 10:38:42

+0

請把你的Facebook API的類,我可以解決你的問題.. 當然.. – 2012-01-10 11:07:06

+0

致命異常:主要 android.content.res.Resources $ NotFoundException:資源ID#0x7f020005 – 2012-01-10 11:31:52

回答

0

我希望就一些細節問題在應用程序崩潰之前收到的錯誤就像Usama所說,logcat會很棒。我看不到你在哪裏初始化變量facebook = new Facebook(APP_ID);.確保你也擁有facebook在清單中使用的所有活動。最後我讓你這個: https://github.com/facebook/facebook-android-sdk/blob/master/examples/simple/src/com/facebook/android/Example.java

我希望這一些幫助。

< <當您嘗試登錄Facebook頁面時,如果您的用戶和密碼正確,您將收到一個令牌,這意味着該應用程序有一個有效的用戶登錄到Facebook服務器。 SessionStore類將該令牌保存在共享首選項中。因此,您可以稍後再使用令牌連接到Facebook,而無需向用戶詢問密碼。只有在會話未過期的情況下才可以這樣做。 關於錯誤: 「當找不到請求的資源時,資源API拋出此異常。」

很久以前,我做了facebook整合,對於一家公司來說,我無法將代碼傳給你,因爲我沒有。看看這篇文章可能會有所幫助。 http://www.jiahaoliuliu.com/2011/05/android-runtime-error.html

我會環顧四周,看看能否找到更明確的東西。

+0

我不明白SessionStore類,因爲它是不存在於我正在使用的facebook_sdk.jar中。 – 2012-01-10 11:27:55

+0

致命例外:main android.content.res.Resources $ NotFoundException:資源ID#0x7f020005 – 2012-01-10 11:32:23

0
android.content.res.Resources$NotFoundException 

有時會被解僱,因爲在Idea中您可能會在導入項目時覆蓋英文字符串資源。