2010-11-18 89 views
17

我使用Facebook的SDK,並與例如與SDK給予的幫助,我公司開發的代碼登錄及的access_token存儲到數據庫如何發佈在Facebook上牆使用Facebook的Android SDK中,而無需打開對話框

我有2問題:

1)。當我嘗試從數據庫中獲取access_token並將其傳遞給Facebook時,它不允許我使用該facebook sdk給出的示例在牆上發帖,爲什麼這樣呢? 2)。我經歷了facebook.java代碼,但是我得到的是在牆上張貼我打開對話框,因爲沒有其他方法可以直接傳遞我的消息併發布。請告訴我朝它或電話我笏當我想張貼在牆壁上的我應該做的,而無需打開一個對話框

mPostButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       String message = "Post this to my wall"; 

       Bundle params = new Bundle();    

       params.putString("message", message); 

       mAsyncRunner.request("me/feed", params, "POST", new WallPostRequestListener()); 

      } 
     }); 

public class WallPostRequestListener extends BaseRequestListener { 

     public void onComplete(final String response) { 
      Log.d("Facebook-Example", "Got response: " + response); 
      String message = "<empty>"; 
      try { 
       JSONObject json = Util.parseJson(response); 
       message = json.getString("message"); 
      } catch (JSONException e) { 
       Log.w("Facebook-Example", "JSON Error in response"); 
      } catch (FacebookError e) { 
       Log.w("Facebook-Example", "Facebook Error: " + e.getMessage()); 
      } 
      final String text = "Your Wall Post: " + message; 
      Example.this.runOnUiThread(new Runnable() { 
       public void run() { 
        mText.setText(text); 
       } 
      }); 
     } 
    } 

哪些錯誤與上面的代碼..它不是在牆壁上張貼,並給我的牆後的解決方案:空對手機和模擬器都提前

謝謝..

+0

這似乎是如此容易,但我嘗試了Ankit的代碼,並得到以下錯誤: 02-02 16:52:58.672:WARN/Bundle(6774):Key access_token expected byte [] but value is a java.lang.String。返回了默認值。 爲什麼? – 2011-02-10 02:20:02

+0

在做**響應= mFacebook.request(「me/feed」,參數,「POST」)之前,你似乎還沒有恢復Facebook對象中的Access_Token和Token_Exprires值; **因此解決方案可能是** SessionStore.restore(mFacebook,this); **(在facebook示例中給出的文件名SessionStore中給出的方法)或者其他** FacebookObject.setAccessToken(由Facebook提供的恢復access_token); FacebookObject.setAccessExpires(恢復facebook提供的access_expires); ** – Ankit 2011-02-10 02:20:02

回答

37

我申請下面的代碼,並可以成功地發佈我的消息在牆上。

public void postOnWall(String msg) { 
     Log.d("Tests", "Testing graph API wall post"); 
     try { 
       String response = mFacebook.request("me"); 
       Bundle parameters = new Bundle(); 
       parameters.putString("message", msg); 
       parameters.putString("description", "test test test"); 
       response = mFacebook.request("me/feed", parameters, 
         "POST"); 
       Log.d("Tests", "got response: " + response); 
       if (response == null || response.equals("") || 
         response.equals("false")) { 
        Log.v("Error", "Blank response"); 
       } 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 
+1

然而,即時通訊尋找一種解決方案,使牆上張貼,從另一個活動,當我試圖從牆上發佈消息從其他活動,它給我一個錯誤,處理消息和所有。 – Ankit 2010-12-07 12:15:53

+0

我也找到了解決方案,我只是通過調用「SessionStore.restore(mFacebook,this)」從我想要發佈帖子的頁面恢復了我的Facebook會話;「 (在facebook例子中給出的文件名SessionStore中給出的方法) – Ankit 2010-12-15 07:15:56

+1

嗨Ankit,你能告訴我你怎麼在調用這個函數之前授權用戶。我嘗試創建一個Facebook對象傳遞應用程序ID,然後調用授權功能,登錄框出現,但登錄後,沒有反應 – Mako 2011-01-22 07:57:20

2

那麼它不是的東西在牆上貼得到不通知用戶。當用戶允許你的應用程序時,那麼Android Facebook sdk將會展示另一個頁面,其中有一個應用程序發送的文本,以及一個用戶可以在他的牆上寫字的文本框,類似於我附加的屏幕截圖

實際佈局在移動設備上略有不同,但格式相同。這個過程在facebook android sdk的示例中已經很好地顯示出來了。

現在檢查在這篇文章中提出的問題:

Facebook連接的Android - 使用stream.publish @ http://api.facebook.com/restserver.php'>Facebook Connect Android -- using stream.publish @ http://api.facebook.com/restserver.php

在這個問題尋找這些:postParams.put(),在你的一些JAVA文件中會出現類似的行。這些是您可以將數據發佈到Facebook的線路。

例如:

postParams.put("user_message", "TESTING 123"); 

是消息,

postParams.put("attachment", "{\"name\":\"Facebook Connect for Android\",\"href\":\"http://code.google.com/p/fbconnect-android/\",\"caption\":\"Caption\",\"description\":\"Description\",\"media\":[{\"type\":\"image\",\"src\":\"http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone/\"}],\"properties\":{\"another link\":{\"text\":\"Facebook home page\",\"href\":\"http://www.facebook.com\"}}}");

是你在哪裏應用,描述,標題,標題等

提供圖標的線

alt text

+0

嗨viv,謝謝你的回覆。但是,比起我在應用程序中如何處理這個問題,當用戶點擊特定的按鈕時,在用Facebook進行身份驗證之後,他的牆上應該貼上一些東西,而不會提示他。 – Ankit 2010-11-18 12:34:12

+0

我編輯了答覆,請檢查.... – viv 2010-11-18 15:09:02

+0

我無法獲得此窗口..我應該設計此窗口...或者如果有任何程序,然後請讓我知道..謝謝 – 2012-07-06 05:37:24

1

我使用Ankit的代碼張貼在Facebook牆上,但他的代碼給我錯誤android.os.NetworkOnMainThreadException

經過對這個問題的搜索後,一個解決方案告訴我,把你的代碼放在AsyncTask中可以擺脫這個問題。修改他的代碼後,它對我來說工作得很好。

修改後的代碼看起來像:

public class UiAsyncTask extends AsyncTask<Void, Void, Void> { 

    public void onPreExecute() { 
     // On first execute 
    } 

    public Void doInBackground(Void... unused) { 
     // Background Work 
     Log.d("Tests", "Testing graph API wall post"); 
     try { 
       String response = facebook.request("me"); 
       Bundle parameters = new Bundle(); 
       parameters.putString("message", "This test message for wall post"); 
       parameters.putString("description", "test test test"); 
       response = facebook.request("me/feed", parameters, "POST"); 
       Log.d("Tests", "got response: " + response); 
       if (response == null || response.equals("") || response.equals("false")) { 
        Log.v("Error", "Blank response"); 
       } 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    public void onPostExecute(Void unused) { 
     // Result 
    } 
} 
+0

謝謝Deepak,你應該總是在與Web服務/服務器通信時使用線程/異步任務,所以間接地在Twitter上使用線程來完成牆上或twit上的帖子。感謝您指出這一點。 – Ankit 2012-08-17 05:19:26

1

這個類可以幫助我在我的Facebook牆上發送消息,而無需對話框:

public class FBManager{ 

    private static final String FB_ACCESS_TOKEN = "fb_access_token"; 
    private static final String FB_EXPIRES = "fb_expires"; 

    private Activity context; 
    private Facebook facebookApi; 

    private Runnable successRunnable=new Runnable(){ 
    @Override 
    public void run() { 
     Toast.makeText(context, "Success", Toast.LENGTH_LONG).show(); 
    } 
    };  

    public FBManager(Activity context){ 

    this.context = context; 

    facebookApi = new Facebook(FB_API_ID); 

    facebookApi.setAccessToken(restoreAccessToken()); 

    } 

    public void postOnWall(final String text, final String link){ 
    new Thread(){ 
     @Override 
     public void run(){ 
      try { 
       Bundle parameters = new Bundle(); 
       parameters.putString("message", text); 
       if(link!=null){ 
        parameters.putString("link", link); 
       } 
       String response = facebookApi.request("me/feed", parameters, "POST"); 
       if(!response.equals("")){ 
        if(!response.contains("error")){ 
         context.runOnUiThread(successRunnable);      
        }else{ 
         Log.e("Facebook error:", response); 
        } 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }.start();  
    } 


    public void save(String access_token, long expires){ 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
    Editor editor=prefs.edit(); 
    editor.putString(FB_ACCESS_TOKEN, access_token); 
    editor.putLong(FB_EXPIRES, expires); 
    editor.commit(); 
    } 

    public String restoreAccessToken(){ 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
    return prefs.getString(FB_ACCESS_TOKEN, null);  
    } 

    public long restoreExpires(){ 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
    return prefs.getLong(FB_EXPIRES, 0);   
    } 

}

相關問題