Monday, November 26, 2012

Posting Message to Facebook Wall in android



Check the previous post "How to integrate Facebook in android"

write a click event for post to wall button and inside click event write a function namedpostToWall()
btnPostToWall.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        postToWall();
    }
});
and function body for postToWall() function is:
public void postToWall() {
    // post on user's wall.
    facebook.dialog(this, "feed", new DialogListener() {
        @Override
        public void onFacebookError(FacebookError e) {
        }
        @Override
        public void onError(DialogError e) {
        }
        @Override
        public void onComplete(Bundle values) {
        }
        @Override
        public void onCancel() {
        }
    });
}
android facebook posting to wall
android facebook posting to wall test

Getting Profile Information from Facebook

To get profile information we need to make an api request to facebook graph API. Following is a function that will make an api request to facebook profile graph api and will get profile information from facebook.
getProfileInformation()
public void getProfileInformation() {
    mAsyncRunner.request("me", new RequestListener() {
        @Override
        public void onComplete(String response, Object state) {
            Log.d("Profile", response);
            String json = response;
            try {
                JSONObject profile = new JSONObject(json);
                // getting name of the user
                String name = profile.getString("name");
                // getting email of the user
                String email = profile.getString("email");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void onIOException(IOException e, Object state) {
        }
        @Override
        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
        }
        @Override
        public void onMalformedURLException(MalformedURLException e,
                Object state) {
        }
        @Override
        public void onFacebookError(FacebookError e, Object state) {
        }
    });
}
The above function will get json data from facebook. You need to parse the json in order to get individual profile data. If you are not aware of json parsing look at this article. Android JSON Parsing Tutorial.
The sample profile json from facebook will be like this
{
   "id": "1464730016",
   "name": "Ravi Tamada",
   "first_name": "Ravi",
   "last_name": "Tamada",
   "username": "ravi8x",
   "birthday": "12/22/1988",
   "hometown": {
      "id": "112158005464147",
      "name": "Baruva"
   },
   "location": {
      "id": "102186159822587",
      "name": "Chennai, Tamil Nadu"
   },
   "bio": "Author: www.androidhive.info\r\nCo-author: www.9lessons.info",
   "work": [
      {
         "employer": {
            "id": "179366562092719",
            "name": "ByteAlly"
         },
         "location": {
            "id": "102186159822587",
            "name": "Chennai, Tamil Nadu"
         },
         "position": {
            "id": "124917314217511",
            "name": "Product Head"
         }
         ]
      }
   ],
   "favorite_athletes": [
      {
         "id": "18620649907",
         "name": "Virat Kohli"
      }
   ],
   "education": [
      {
         "school": {
            "id": "131587206873093",
            "name": "Raghu Engineering College (REC)"
         },
         "degree": {
            "id": "140065339390579",
            "name": "B.Tech"
         },
         "year": {
            "id": "142963519060927",
            "name": "2010"
         },
         "type": "Graduate School",
         "classes": [
            {
               "id": "192259410803415",
               "name": "2010",
               "with": [
                  {
                     "id": "584960408",
                     "name": "Santosh Patnaik"
                  }
               ],
               "from": {
                  "id": "584960408",
                  "name": "Santosh Patnaik"
               }
            }
         ]
      }
   ],
   "gender": "male",
   "relationship_status": "Single",
   "website": "www.androidhive.info\nwww.9lessons.info\nwww.twitter.com/ravitamada\nwww.about.me/rv",
   "timezone": 5.5,
   "locale": "en_US",
   "languages": [
      {
         "id": "106059522759137",
         "name": "English"
      },
      {
         "id": "107617475934611",
         "name": "Telugu"
      },
      {
         "id": "112969428713061",
         "name": "Hindi"
      },
      {
         "id": "343306413260",
         "name": "Tamil"
      }
   ],
   "verified": true,
   "updated_time": "2012-03-02T17:04:18+0000"
}

Extending facebook Permissions

If you want user’s other information like checkins, friends list etc., you need to extend facebook permissions while logging in user. Check list of facebook permissions
facebook.authorize(this, new String[] { "email", "publish_checkins", "publish_stream" },
      new DialogListener() {
           @Override
           public void onComplete(Bundle values) {}
           @Override
           public void onFacebookError(FacebookError error) {}
           @Override
           public void onError(DialogError e) {}
           @Override
           public void onCancel() {}
      }
);

Getting Access Token

Sometimes you might needed users access token for future purpose usage. Following code will give you currently logged in user access token.
String access_token = facebook.getAccessToken();

Logout from your app

When user want to stop using facebook for your app, you can provide logout method to clear app state and invalidate access token. So further you can’t make request to facebook from your app.
logoutFromFacebook();
public void logoutFromFacebook() {
    mAsyncRunner.logout(this, new RequestListener() {
        @Override
        public void onComplete(String response, Object state) {
            Log.d("Logout from Facebook", response);
            if (Boolean.parseBoolean(response) == true) {
                // User successfully Logged out
            }
        }
        @Override
        public void onIOException(IOException e, Object state) {
        }
        @Override
        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
        }
        @Override
        public void onMalformedURLException(MalformedURLException e,
                Object state) {
        }
        @Override
        public void onFacebookError(FacebookError e, Object state) {
        }
    });
}