2012-01-10 86 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中您可能会在导入项目时覆盖英文字符串资源。