2010-11-04 58 views
4

我遇到问题了。我想使用facebook api并在没有调用对话框的情况下发布到我的墙上。基本上我有一个应用程序,我希望人们能够共享应用程序,所以我想要发布特定的消息。我不断收到“方法未执行”的回复。下面是该帖子的代码。android facebook api post

//I tried this also ->>String path = "http://graph.facebook.com/me/feed"; 
String path = "https://api.facebook.com/method/stream.publish"; 
Bundle b = new Bundle(); 
//And i tried this -> b.putString("access_token",facebook.getAccessToken()); 
b.putString("message", "this is just a test..."); 
try { 
    String ret = facebook.request(path, b); 
    Toast.makeText(fmasterActivity.this, ret, Toast.LENGTH_LONG).show(); 
    } catch (MalformedURLException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 

回答

9

我假设你在用户成功验证后正在执行那段代码?

这段代码为我工作:

private Facebook mFacebook; 
private AsyncFacebookRunner mAsyncRunner; 

private void onFacebookShare() { 
    mFacebook = new Facebook(); 
    mAsyncRunner = new AsyncFacebookRunner(mFacebook); 

    SessionEvents.addAuthListener(new SampleAuthListener()); 
    SessionEvents.addLogoutListener(new SampleLogoutListener()); 
} 

private void postToFBWall() { 
    if(mFacebook.isSessionValid()){ 
     shareVideoOnFB(); 
    } else { 
     showDialog(DIALOG_FBOOK_LOGIN); 
    } 
} 

public void shareVideoOnFB(){ 
    Bundle params = new Bundle(); 
    params.putString("message", "This string will appear as the status message"); 
    params.putString("link", "This is the URL to go to"); 
    params.putString("name", "This will appear beside the picture"); 
    params.putString("caption", "This will appear under the title"); 
    params.putString("description", "This will appear under the caption"); 
    params.putString("picture", "This is the image to appear in the post"); 

    mAsyncRunner.request("me/feed", params, "POST", new RequestListener() { 
     public void onMalformedURLException(MalformedURLException e) {} 
     public void onIOException(IOException e) {} 
     public void onFileNotFoundException(FileNotFoundException e) {} 
     public void onFacebookError(FacebookError e) {} 
     public void onComplete(String response) { 
      logoutFacebook(); 
     } 
    }); 

    Toast.makeText(ShareActivity.this, "Posting to your Wall...", Toast.LENGTH_SHORT).show();  
} 

您可以在活动的onCreate()调用onFacebookShare(),然后当用户按下任何以表明他/她想要在Facebook上分享,调用postToFBWall()。当然,您必须添加处理以显示登录对话框。

+2

谢谢你的权利。我已经知道了,但我很感激。对于android facebook sdk来说,可悲的是很少有文档。同样给一位老太太一台笔记本电脑,并告诉她“祝你好运,玩得开心”。 – 2010-11-05 10:56:31

+2

的确如此。我花了几个小时,很多挫折才弄明白了这一点。 – Zarah 2010-11-06 07:56:41