Social Icons

2010年11月3日 星期三

使用facebook api

在我的程式中有使用facebook api,雖然不是很難,不過也把我使用的方法紀錄和分享下來。

  • login和授權:
facebook api已經將登入和授權整個UI都包好了,使用時也是非常方便。
mFacebook = new Facebook;
...
        if (mFacebook.isSessionValid() == false) {
         mFacebook.authorize(ctx, APP_ID, FB_PERMISSIONS, new DialogListener() {
    @Override
    public void onCancel() {
    }
    @Override
    public void onComplete(Bundle values) {
     post();
    }
    @Override
    public void onError(DialogError e) {
     sendNotify(false);
    }
    @Override
    public void onFacebookError(FacebookError e) {
     sendNotify(false);
    }
         });
        } else {
         post();
        }
當mFacebook.isSessionValid()為true時代表授權還沒有過期,是不需要在要求使用者登錄的,可以繼續我們上傳的程序。

  • Post message:
下面是post message的範例。
  final String GRAPH_PATH = "me/feed";
  final String POST_MESSAGE = "message";
  Bundle params = new Bundle();
  String msgVal = null;
  if (this.msg != null) {
   msgVal = this.title+"\n"+this.msg;
  } else {
   msgVal = this.title;
  }
  params.putString(POST_MESSAGE, msgVal);
  
  try {
   String resp = mFacebook.request(GRAPH_PATH, params, "POST");
   JSONObject json = Util.parseJson(resp);
   postId = json.getString("id");

  • Post photo:
重點在於如何將photo變成binary傳送到facebook;另外縮圖也很重要,facebook上傳照片的時間很長,如果能先縮圖可以減少上傳時間。
  final String GRAPH_PATH = "me/photos";
  final String POST_MESSAGE = "message";
  Bundle params = new Bundle();
  
  String msgVal = null;
  if (this.msg != null) {
   msgVal = this.title+"\n"+this.msg;
  } else {
   msgVal = this.title;
  }
  params.putString(POST_MESSAGE, msgVal);
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  travelMap.compress(Bitmap.CompressFormat.PNG, 100, out);
  params.putByteArray("picture", out.toByteArray());   
        
  try {
   String resp = mFacebook.request(GRAPH_PATH, params, "POST");
   JSONObject json = Util.parseJson(resp);
   postId = json.getString("id");

  • Thread:
因為facebook api會花一些時間等待,所以最好把整個程序放在背景執行會比較好。
    private void post() {
     Thread thread = new Thread()
     {
         @Override
         public void run() {
          boolean bRet = false;
      switch(hi.getType()) {
      case HistoryDB.HISTORY_PHOTO_TYPE:
       bRet = postPhoto();
       break;
           
      case HistoryDB.HISTORY_POI_TYPE:
       bRet = postFeed();
       break;
           
      default:
       break;
      }
          
          Message msg = new Message();
          if (bRet == true)
           msg.arg1 = 1;
          else
           msg.arg1 = 0;
          handler.sendMessage(msg);
         }
     };
     thread.start();
    }
    
    Handler handler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
         if (msg.arg1 == 1) {
          sendNotify(true);
         } else {
          sendNotify(false);
         }
        }
    };

4 則留言:

  1. 你好,自己也在學這一塊,但是初學在上傳照片這一塊出現很多錯誤
    運用了
    https://github.com/facebook/facebook-android-sdk的範例後修改了內建從網路照片傳入facebook到從手機內建照片出現許多錯誤

    不知可否題供範例參考,萬分感謝!!

    回覆刪除
  2. 不知可否告知發生什麽樣的錯誤呢?這樣比較好幫你看看,因為你程式所需的範例,我都寫出來了。

    回覆刪除
  3. 你好,
    我們在看了許多程式碼後
    postId = json.getString("id");
    這段的postId確發生無法回傳的錯誤。

    謝謝您的回覆^^

    回覆刪除
  4. 請問您圖片上傳時,圖片會每次都出現在塗鴉牆上嗎?

    回覆刪除