2010-11-04 173 views
9

我想能夠使用facebook android sdk併發布鏈接到Facebook。我想要的一個例子是,如果你在Facebook上,並且在你的狀態部分輸入一個鏈接,比如「http://www.google.com」。當你這樣做時,一個盒子彈出,你的文章結束成爲一個有圖像和鏈接的塊。我在facebook api上找到了使用attatchment的文檔,但是當我嘗試使用android facebook api來做這件事時,它似乎不起作用。我在網上找了幾個小時,沒有運氣。謝謝。android facebook api發佈到圖像牆

+0

你可以發佈你的當前代碼嗎? – Zarah 2010-11-05 09:17:25

+0

想通了。 – 2010-11-05 10:56:58

+0

也許你可以分享你的解決方案? – 2010-11-10 19:28:09

回答

16

Asuming當你讀到這,你知道如何登錄到通過API Facebook和這樣...

private void fbImageSubmit(Facebook fb, String imageurl, String caption, String description, String name, String linkurl) 
    { 
     if(fb != null) 
     { 
      if(fb.isSessionValid()) 
      { 
       Bundle b = new Bundle(); 
       b.putString("picture", imageurl); 
       b.putString("caption",caption); 
       b.putString("description",description); 
       b.putString("name",name); 
       b.putString("link",linkurl); 
       try { 
        String strRet = ""; 
        strRet = fb.request("/me/feed",b,"POST"); 
        JSONObject json; 
        try { 
         json = Util.parseJson(strRet); 
         if(!json.isNull("id")) 
         { 
          Log.i("Facebook", "Image link submitted."); 
         } 
         else 
         { 
          Log.e("Facebook","Error: " + strRet); 
         } 
        } catch (FacebookError e) { 
         Log.e("Facebook","Error: " + e.getMessage()); 
        } 
       } catch (Exception e) { 
        Log.e("Facebook", "Error: " + e.getMessage()); 
       } 
      } 
     } 
    } 
+1

+1 Thanx,幫助我知道一些參數名稱要捆綁傳遞。 – 2011-06-06 06:04:58

+1

這種方法完全可以工作..好工作@ Kelton – 2012-06-21 16:26:39

+0

imageUrl和LinkUrl?他們是什麼? – 2013-07-24 12:10:32

3

這工作完美罰款進度對話框..我已經用它...

您必須加入了Facebook的罐子...

Facebook authenticatedFacebook = new Facebook(APP_ID); 

    private static final String[] PERMISSIONS = new String[] { "publish_stream", "read_stream", "offline_access" }; 

撥打以下功能按鈕點擊....

authenticatedFacebook.authorize(YOUR_CLASS_NAME.this, PERMISSIONS, new FaceBookWallPostListener()); 

現在加入這個類...

public class FaceBookWallPostListener implements DialogListener { 

    public void onComplete(Bundle values) { 
     new FacebookWallPost().execute(); 
    } 

    public void onCancel() { 
    } 

    public void onError(DialogError e) { 
     e.printStackTrace(); 
    } 

    public void onFacebookError(FacebookError e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
protected boolean isRouteDisplayed() { 
    return false; 
} 





private class FacebookWallPost extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... params) { 
     try { 
      path = "Path OF YOUR IMAGE"; 
      Bundle parameters = new Bundle(); 
      parameters.putString("message", "MESSAGE YOU WANT TO POST"); 
      try { 
       File file = new File(path, "IMAGE_NAME.jpg"); 
       Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); 
       byte[] data = null; 
       ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
       data = baos.toByteArray(); 
       if (data != null) { 
        parameters.putByteArray("picture", data); 
       } 
       parameters.putString("access_token", authenticatedFacebook.getAccessToken()); 
       authenticatedFacebook.request("me"); 
       authenticatedFacebook.request("me/photos", parameters, "POST"); 
      } catch (Exception e) { 
       return e.getMessage(); 
      } 

      return "success"; 
     } catch (Exception e) { 
      return e.getMessage(); 
     } 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     pDialog.dismiss(); 
     if (result.equals("success")) { 
      Toast.makeText(YOUR_CLASS_NAME.this, "WallPost Successfully Done", Toast.LENGTH_SHORT).show(); 
      try { 
       new File(Environment.getExternalStorageDirectory().toString() + "/Diegodeals", "diegodeals.jpg").delete(); 
      } catch (Exception e) { 
      } 

     } else { 
      Toast.makeText(YOUR_CLASS_NAME.this, "Failed to post \n " + result, Toast.LENGTH_SHORT).show(); 
     } 

    } 

    @Override 
    protected void onPreExecute() { 
     pDialog = new ProgressDialog(YOUR_CLASS_NAME.this); 
     pDialog.setMessage("Posting Picture & Message on Facebook..."); 
     pDialog.show(); 
    } 

} 

///// GOOOD運氣。