Browse Month: May 2014

How to Login via Facebook,get facebook friendlist and their details, post on friends wall and get newsfeeds details

How to Login with Facebook:

follow the below steps to add login via facebook:

step1: register app on developers.facebook.com

step 2: get the app_id , add it to strings file and add following code to manifest file:

<meta-data

android:name=”com.facebook.sdk.ApplicationId”

android:value=”@string/app_id” />

//add meta-data inside appication tag.

<activity android:name=”com.facebook.LoginActivity” >
</activity>

step 3: Download latest facebooksdk and import it as project in your project

step 4: add following code onclick of login with facebook button:

try {

PackageInfo info = getPackageManager().getPackageInfo( “package_name”, PackageManager.GET_SIGNATURES);

for (Signature signature : info.signatures)

{

MessageDigest md = MessageDigest.getInstance(“SHA”); md.update(signature.toByteArray());

Log.d(“KeyHash:”, Base64.encodeToString(md.digest(), Base64.DEFAULT));

}

} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
} //

 

//Get keyhash from above code and add it to developers.facebook in your project
session1 = Session.getActiveSession();

if (session1 == null)

{ session1 = new Session(Login.this);

Session.setActiveSession(session1);

if (session1.getState().equals( SessionState.CREATED_TOKEN_LOADED))

{

session1.openForRead(new Session.OpenRequest(Login.this) .setCallback(statusCallback));

access_token = session1.getAccessToken();

}

}

if (!session1.isOpened() && !session1.isClosed())

{

session1.openForRead(new Session.OpenRequest(Login.this) .setCallback(statusCallback));

access_token = session1.getAccessToken();

} else

{

Session.openActiveSession(Login.this, true, statusCallback);

access_token = session1.getAccessToken();

}

}

/// Add this method to your code:

private Session.StatusCallback statusCallback = new Session.StatusCallback() {
@Override public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

public void onCompleted(GraphUser user, Response response) {
if (user != null) {

user.getId();

fbid = user.getId();

fbname = user.getName();
} } });
} }
};

 

//override this method of activity:

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

Session.getActiveSession() .onActivityResult(this, requestCode, resultCode, data);

}

 

–>How to Get Facebook friendlist:

Create async task and use below url to get facebookfriend list:

https://graph.facebook.com/me/friends?access_token=”session access_token”

Note: parse the data using Json .

–>How to share on friends wall:

add below code to share on friends wall:

private void publishFeedDialog(int position) {

try{

Bundle params = new Bundle();

params.putString(“message”,”YOUR MESSAGE” );

params.putString(“to”,”Friends userid”);

WebDialog feedDialog = (   new WebDialog.FeedDialogBuilder(Context,  Session.getActiveSession(),    params))                .setOnCompleteListener(null)   .build();

feedDialog.show();

} catch(Exception e){

e.printStackTrace(); }

}

 

–> How to Get NewsFeed:

Use below url to get newsfeed:

link: https://graph.facebook.com/me/home?access_token=access_token

Note: use Json parsing to retrive details of each newsfeed.

 

Loading image from url using AQuery

Download android-query.jar from below link:

http://www.java2s.com/Code/Jar/a/Downloadandroidqueryfull0243jar.htm

 

Add following code where you want to load image :

public ImageLoader imageLoader;

imageLoader = new ImageLoader(this);

AQuery androidAQuery=new AQuery(getApplicationContext());

androidAQuery.id(ImageView).image(url, true, true, 100,default_pic);

 

Note: if you dont want to store image in cache memory set true parameters to false.