Friday 14 September 2012

Android Facebook Integration



Hello Friends,
               This tutorial is about integrating facebook into your android   
               application. 
              
     
               Step 1: Registering Your Facebook Application:
               Go to facebook developer app site and Create the application.
               create new facebook application and fill out all the information 
               needed. And select Native Android App.
               And  note down your facebook App ID.
              
     
           Step 2: Creating Facebook Reference Project :
                         Once you are done with registering your facebook 
           application,    you need to download facebook SDK and create 
           a new library project. This reference project will be used to
           compile your actual project.
                    1. Download facebook android SDK from git repo-
               sitories.


          2. In your Eclipse goto File ⇒ Import ⇒ Existing Projects
              into Workspace and select the facebook project you downloaded
              from git repository.
                                  








       Now, You are ready to run the project  :) .
    
       Screen Shot of my project:











   Code:     
   1. Get Profile Information:
        
          

        /**
  * Get Profile information by making request to Facebook Graph API
  * */
 public void getProfileInformation() {
  mAsyncRunner.request("me", new RequestListener() {
   @Override
   public void onComplete(String response, Object state) {
    Log.d("Profile", response);
    String json = response;
    try {
     // Facebook Profile JSON data
     JSONObject profile = new JSONObject(json);
     
     // getting name of the user
     final String name = profile.getString("name");
     
     // getting email of the user
     final String email = profile.getString("email");
     
     // getting user name of the user
     username = profile.getString("username");
     
     runOnUiThread(new Runnable() {

      @Override
      public void run() {
       Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email +"\nUserName : "+username, 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) {
   }
  });
 }
 
2.Get All my Facebook Friends :
 
 
 mAsyncRunner.request(username+"/picture", new RequestListener() {
   @Override
   public void onComplete(String response, Object state) {
    Log.d("Profile", response);
    String json = response;
    try {
     // Facebook Profile JSON data
     JSONObject profile = new JSONObject(json);
     JSONArray arrayFriend = profile.getJSONArray("data");
     Log.d("Profile======", arrayFriend.toString());
     // getting name of the user
     
     if (arrayFriend != null) {
               for (int i = 0; i < arrayFriend.length(); i++) {
                   String name = arrayFriend.getJSONObject(i).getString("name");

                   String id = arrayFriend.getJSONObject(i).getString("id");

                   Log.i(name,id);
                   
               }
           } 
     final String name = profile.getString("name");
     
     // getting email of the user
     final 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();
   }
 
  3. Getting my facebook profile Picture
 
  
    Code:
 
    ImageView user_picture;
    user_picture=(ImageView)findViewById(R.id.imageView);
    URL url;
    try {
 url = new URL("http://graph.facebook.com/"+username+"/picture?type=large");
        Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
 user_picture.setImageBitmap(bmp);
 } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
     e.printStackTrace();
 } catch (IOException e) {
   // TODO Auto-generated catch block
    e.printStackTrace();
 } 
 


   Hope , The above tutorial Helps Anyone...
    Enjoy Coding... :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

15 comments:

  1. Donde puedo bajarme este proyecto de ejemplo?

    ReplyDelete
  2. Thanks, may be the link is broken.
    I will update the download link soon...

    ReplyDelete
  3. Can you please attach the code ?
    Link is broken again it seems :(

    ReplyDelete
  4. Thanks,
    Sure, I will Do that ASAP.

    ReplyDelete
  5. the Link is still broken .. could you please attach the source code

    ReplyDelete
  6. Great work.could you please arrange a link for source code.

    ReplyDelete
  7. Hi Sir please provide ur code with me..at guru2010j@gmail.com

    ReplyDelete
  8. I am new to android..Please send the Source Code

    ReplyDelete
  9. hello dear i m very new to development.. ur blog helping a lot .. keep it up mn pls continue to work on this blog....:-)

    ReplyDelete
  10. Tôi mới vào Android .. Xin vui lòng gửi Mã nguồn
    email: nguyenthienduc10i2@gmail.com

    ReplyDelete
  11. Hi! your code works perfectly in a Motorola device, but in Samsung device it don´t work,I don´t know, what is the problem, but i think the problem is in sdk versión. Can you help with new versión api facebook for android?
    Thanks.

    ReplyDelete
  12. facebook sdk 3.7 integration with fbfriend in listview
    listview (profile pic,name)
    plzzz

    ReplyDelete
  13. I have tried the same code but getting this in my logcat- W/System.err(2217): org.json.JSONException: No value for name

    ReplyDelete
  14. I have tried the same code but getting something like this in my logcat-jsonParsing-name not found.

    ReplyDelete
  15. Can you please share your code to me i am not able to do this
    please share your code

    ReplyDelete

 

Copyright @ 2013 Android Developers Blog.