2011-03-02 36 views

回答

3

您應該添加「動作」的請求參數:

JSONStringer actions; 
try { 
    actions = new JSONStringer().object() 
       .key("name").value("Click me!") 
       .key("link").value("http://stackoverflow.com/").endObject(); 
    params.putString("actions", actions.toString()); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 
+0

完美的代碼! – Jonno 2012-03-10 05:46:32

+0

嗨我綁你的代碼發佈像{「名稱」:「點擊我!」,「鏈接」:「http://stackoverflow.com/」}你能幫我從這個請... – GoCrazy 2012-07-16 12:19:32

+0

非常感謝,它爲我工作:) – Kunu 2014-08-04 06:04:15

6

我喜歡抄襲了Facebook SDK源代碼,以我自己的Eclipse的Android項目,簡化調試。我的代碼基於Facebook SDK附帶的簡單示例。所以如果你想複製我的代碼,請確保從示例項目中添加一些類!

我更喜歡使用JSONObject創建我的帖子,這有助於我保持我的代碼清潔。在帖子真的被髮送到用戶的牆上之前,我彈出一個對話框,使用戶能夠看到他的牆上會發生什麼。如果用戶喜歡,用戶也可以添加自己的信息。


    private void publishPhoto(String imageURL) { 
     Log.d("FACEBOOK", "Post to Facebook!"); 

     try { 

      JSONObject attachment = new JSONObject(); 
      attachment.put("message", Utils.s(R.string.fb_message)); 
      attachment.put("name", Utils.s(R.string.fb_name)); 
      attachment.put("href", Utils.s(R.string.url_dotzmag)); 
      attachment.put("description", Utils.s(R.string.fb_description)); 

      JSONObject media = new JSONObject(); 
      media.put("type", "image"); 
      media.put("src", imageURL); 
      media.put("href", Utils.s(R.string.url_dotzmag)); 
      attachment.put("media", new JSONArray().put(media)); 

      JSONObject properties = new JSONObject(); 

      JSONObject prop1 = new JSONObject(); 
      prop1.put("text", "Dotz App on Android Market"); 
      prop1.put("href", Utils.s(R.string.url_android_market)); 
      properties.put("Get the App for free", prop1); 

      JSONObject prop2 = new JSONObject(); 
      prop2.put("text", "Dotz Tuning on Facebook"); 
      prop2.put("href", Utils.s(R.string.url_facebook_fanpage)); 
      properties.put("Visit our fanpage", prop2); 

      attachment.put("properties", properties); 

      Log.d("FACEBOOK", attachment.toString()); 

      Bundle params = new Bundle(); 
      params.putString("attachment", attachment.toString()); 
      mFacebook.dialog(mActivity, "stream.publish", params, new PostPhotoDialogListener()); 
      //mAsyncRunner.request("me/feed", params, "POST", new WallPostRequestListener(), null); 

     } catch (JSONException e) { 
      Log.e("FACEBOOK", e.getLocalizedMessage(), e); 
     } 
    } 

    public class PostPhotoDialogListener extends BaseDialogListener { 

     public void onComplete(Bundle values) { 
      final String postId = values.getString("post_id"); 
      if (postId != null) { 
       Log.d("FACEBOOK", "Dialog Success! post_id=" + postId); 
       Toast.makeText(mActivity, "Successfully shared on Facebook!", Toast.LENGTH_LONG).show(); 
       /* 
       mAsyncRunner.request(postId, new WallPostRequestListener()); 
       mDeleteButton.setOnClickListener(new OnClickListener() { 
        public void onClick(View v) { 
         mAsyncRunner.request(postId, new Bundle(), "DELETE", 
           new WallPostDeleteListener(), null); 
        } 
       }); 
       */ 
      } else { 
       Log.d("FACEBOOK", "No wall post made"); 
      } 
     } 
    } 
+0

好的答案..謝謝.. – Venky 2011-09-23 09:17:39

+0

nice.but我不想要對話框我發送這樣的請求res = facebook.request(「我/飼料」,PARAMS,「POST」);我得到{「error」:{「message」:「(#100)Missing message or attachment」,「type」:「OAuthException」,「code」:100}}如何解決它 – 2013-03-19 05:10:44

1
private void publishPhoto(String imageURL) { 
    Log.d("FACEBOOK", "Post to Facebook!"); 

    try { 

     JSONObject attachment = new JSONObject(); 
     attachment.put("message","Type your message to share"); 
     attachment.put("name", "Your Application Name")); 
     attachment.put("href", "Any hyperLink"); 
     attachment.put("description","Description about Application"); 

     JSONObject media = new JSONObject(); 
     media.put("type", "image"); 
     media.put("src", "URL path of posting image"); 
     media.put("href","Any hyperLink")); 
     attachment.put("media", new JSONArray().put(media)); 

     JSONObject properties = new JSONObject(); 

     JSONObject prop1 = new JSONObject(); 
     prop1.put("text", "Text or captionText to Post"); 
     prop1.put("href", "Any hyperLink"); 
     properties.put("Get the App for free(or any custom message))", prop1); 

     // u can make any number of prop object and put on "properties" for ex: //prop2,prop3 

     attachment.put("properties", properties); 

     Log.d("FACEBOOK", attachment.toString()); 

     Bundle params = new Bundle(); 
     params.putString("attachment", attachment.toString()); 
     mFacebook.dialog(mActivity, "stream.publish", params, new PostPhotoDialogListener());  

    } catch (JSONException e) { 
     Log.e("FACEBOOK", e.getLocalizedMessage(), e); 
    } 
} 

public class PostPhotoDialogListener extends BaseDialogListener { 

    public void onComplete(Bundle values) { 
     final String postId = values.getString("post_id"); 
     if (postId != null) { 
      Log.d("FACEBOOK", "Dialog Success! post_id=" + postId); 
      Toast.makeText(mActivity, "Successfully shared on Facebook!", Toast.LENGTH_LONG).show(); 

     } else { 
      Log.d("FACEBOOK", "No wall post made"); 
     } 
    } 
} 
+0

我見過的最佳代碼。完美的工作。 – user1220332 2012-02-22 06:56:37

0

你可以試試這個lib中與Facebook通信:

http://restfb.com/

我推薦這個,因爲你不必處理這個JSON的東西;-)

參見本代碼:

// Publishing an image to a photo album is easy! 
// Just specify the image you'd like to upload and RestFB will handle it from there. 

FacebookType publishPhotoResponse = facebookClient.publish("me/photos", FacebookType.class, 
    BinaryAttachment.with("cat.png", getClass().getResourceAsStream("/cat.png")), 
    Parameter.with("message", "Test cat")); 

out.println("Published photo ID: " + publishPhotoResponse.getId()); 

// Publishing a video works the same way. 

facebookClient.publish("me/videos", FacebookType.class, 
    BinaryAttachment.with("cat.mov", getClass().getResourceAsStream("/cat.mov")), 
    Parameter.with("message", "Test cat")); 
+0

你也可以添加其他參數:course.- – radzio 2012-02-22 07:07:24