2010-11-04 56 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