FB Connect di Android

Facebook emang lagi boomingnya sekarang. Apa-apa di connectin sama facebook. Kayak Foursquare dan lain-lain juga bisa di tampilin di FB. Mungkin karena facebook sudah merajalela sampe ke pelosok desa kali ya makanya apa-apa di connectin ke FB. Sekalian bisa sebagai fasilitas buat ngiklanin product juga. Oke qt mau nyoba bagaimana develope aplikasi di android agar bisa terkoneksi ke Facebook.

1. Download Facebook Connect Android

Bisa di dapetin di sini. Kita bisa dapetin bentuk .zip dari link tadi. Silahkan di unzip “fbconnect-android.zip/api”  dan jadiin API ini menjadi .Jar dengan cara :

  • Buka Eclipse
  • File->Import->Existing Project into Workspace. Abis itu tujuan kita menjadi API ini menjadi  .Jar .
  • File->Export->JAVA->Jar File . Masukan nama yg di inginkan untuk membuat .Jar . Atau kalau gak mau ribet. Ane kasih Link nya aja : Silahkan download di sini

2. Tambahkan FBConnect.Jar kedalam project

3. Membuat Applikasi di Facebook

Sebelum memulai coding kita harus membuat aplikasi terlebih dahulu. Aplikasi ini nantinya akan di panggil di android kita dengan cara memasukan KEY dan SECRET. Cara membuat aplikasi di facebook dengan mengakse http://www.facebook.com/developers kemudian klik Set Up New Application teruskan langkahnya sampai aplikasi kita terdaftar di facebook dengan hasil akhir mendapatkan Secret & Key dari aplikasi tersebut.

4. Masukan kode beikut sebagai XML

Misal namanya facebook_android.xml

<!--?xml version="1.0" encoding="utf-8"?-->
	<com.codecarpet.fbconnect.FBLoginButton 		android:id="@+id/login" android:layout_width="fill_parent" 		android:layout_height="wrap_content" />

	<button>

Dan untuk class Activitesnya :

package com.test.exercise;
import java.util.Collections;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.codecarpet.fbconnect.FBDialog;
import com.codecarpet.fbconnect.FBFeedActivity;
import com.codecarpet.fbconnect.FBLoginButton;
import com.codecarpet.fbconnect.FBPermissionActivity;
import com.codecarpet.fbconnect.FBRequest;
import com.codecarpet.fbconnect.FBSession;
import com.codecarpet.fbconnect.FBDialog.FBDialogDelegate;
import com.codecarpet.fbconnect.FBLoginButton.FBLoginButtonStyle;
import com.codecarpet.fbconnect.FBRequest.FBRequestDelegate;
import com.codecarpet.fbconnect.FBSession.FBSessionDelegate;

public class FacebookConnect extends Activity {
 // /////////////////////////////////////////////////////////////////////////////////////////////////
 // This application will not work until you enter your Facebook
 // application's API key here:

 private static final String kApiKey = "Input Your Key Application"; // Masukan key App Facebook

 // Enter either your API secret or a callback URL (as described in
 // documentation):
 private static final String kApiSecret = "Input your secret code"; // Masukan Secret App Facebook
 private static final String kGetSessionProxy = null; // "";
 private static final int PERMISSIONREQUESTCODE = 1;
 private static final int MESSAGEPUBLISHED = 2;
 // /////////////////////////////////////////////////////////////////////////////////////////////////

 private FBSession _session;
 private FBLoginButton _loginButton;
 private TextView _label;
 private Button _permissionButton;
 private Button _feedButton;
 private Handler mHandler;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 mHandler = new Handler();

 if (kGetSessionProxy != null) {
 _session = FBSession.getSessionForApplication_getSessionProxy(
 kApiKey, kGetSessionProxy, new FBSessionDelegateImpl());
 } else {
 _session = FBSession.getSessionForApplication_secret(kApiKey,
 kApiSecret, new FBSessionDelegateImpl());
 }

 setContentView(R.layout.facebook_connect);
 _label = (TextView) findViewById(R.id.label);

 _feedButton = (Button) findViewById(R.id.feedButton);
 _feedButton.setOnClickListener(new OnClickListener() {

 @Override
 public void onClick(View arg0) {
 publishFeed();
 }
 });

 _loginButton = (FBLoginButton) findViewById(R.id.login);
 _loginButton.setStyle(FBLoginButtonStyle.FBLoginButtonStyleWide);
 _loginButton.setSession(_session);
 _session.resume(this);

 }

 private void askPermission() {
 Intent intent = new Intent(this, FBPermissionActivity.class);
 intent.putExtra("permissions", new String[] { "publish_stream" });
 this.startActivityForResult(intent, PERMISSIONREQUESTCODE);
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 // TODO Auto-generated method stub
 super.onActivityResult(requestCode, resultCode, data);

 switch (requestCode) {
 case PERMISSIONREQUESTCODE:
 if (resultCode == 1)
 _permissionButton.setVisibility(View.INVISIBLE);
 default:
 return;
 }

 }

 private void publishFeed() {
 Intent intent = new Intent(this, FBFeedActivity.class);
 intent.putExtra("userMessagePrompt", "Example prompt");
 intent
 .putExtra(
 "attachment",
 "{\"name\":\"Facebook Connect for Android\",\"href\":\"http://code.google.com/p/fbconnect-android/\",\"caption\":\"Caption\",\"description\":\"Description\",\"media\":[{\"type\":\"image\",\"src\":\"http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone/\"}],\"properties\":{\"another link\":{\"text\":\"Facebook home page\",\"href\":\"http://www.facebook.com\"}}}");
 this.startActivityForResult(intent, MESSAGEPUBLISHED);
 }

 // /////////////////////////////////////////////////////////////////////////////////////////////////

 private class FBDialogDelegateImpl extends FBDialogDelegate {

 @Override
 public void didFailWithError(FBDialog dialog, Throwable error) {
 _label.setText(error.toString());
 }

 }

 private void checkPermission() {
 String fql = "select publish_stream from permissions where uid == "
 + String.valueOf(_session.getUid());
 Map<String, String> params = Collections.singletonMap("query", fql);
 FBRequest.requestWithDelegate(new FBHasPermissionRD()).call(
 "facebook.fql.query", params);
 }

 private class FBSessionDelegateImpl extends FBSessionDelegate {

 @Override
 public void session_didLogin(FBSession session, Long uid) {
 // we check if the user already has the permissions before
 // displaying permission button
 checkPermission();

 mHandler.post(new Runnable() {
 public void run() {
 _feedButton.setVisibility(View.VISIBLE);
 }
 });

 String fql = "select uid,name from user where uid == "
 + session.getUid();

 Map<String, String> params = Collections.singletonMap("query", fql);
 FBRequest.requestWithDelegate(new FBRequestDelegateImpl()).call(
 "facebook.fql.query", params);
 }

 @Override
 public void sessionDidLogout(FBSession session) {
 mHandler.post(new Runnable() {
 public void run() {
 _label.setText("");
 _permissionButton.setVisibility(View.INVISIBLE);
 _feedButton.setVisibility(View.INVISIBLE);
 }
 });
 }

 }

 private class FBRequestDelegateImpl extends FBRequestDelegate {

 @Override
 public void request_didLoad(FBRequest request, Object result) {

 String name = null;

 if (result instanceof JSONArray) {
 JSONArray jsonArray = (JSONArray) result;
 try {
 JSONObject jo = jsonArray.getJSONObject(0);
 name = jo.getString("name");
 } catch (JSONException e) {
 e.printStackTrace();
 }
 }
 _label.setText("Logged in as " + name);
 }

 @Override
 public void request_didFailWithError(FBRequest request, Throwable error) {
 _label.setText(error.toString());
 }
 }

 private class FBHasPermissionRD extends FBRequestDelegate {

 @Override
 protected void request_didFailWithError(FBRequest request,
 Throwable error) {
 super.request_didFailWithError(request, error);
 }

 @Override
 public void request_didLoad(FBRequest request, Object result) {
 int hasPermission = 0;

 if (result instanceof JSONArray) {
 JSONArray jsonArray = (JSONArray) result;
 try {
 JSONObject jo = jsonArray.getJSONObject(0);
 hasPermission = jo.getInt("publish_stream");
 if (hasPermission == 0) {
 mHandler.post(new Runnable() {
 public void run() {
 _permissionButton.setVisibility(View.VISIBLE);
 }
 });
 }
 } catch (JSONException e) {
 e.printStackTrace();
 }
 }
 }
 }
}

 

Referensi : http://www.mobisoftinfotech.com/blog/android/845/

Ingin mendapatkan artikel menarik dan lengkap ? Kunjungi situs www.ruangartikel.com

One thought on “FB Connect di Android

  1. lufthi says:

    wew makasih ndra nuxer tutorial android nya …searching2 ketemu si hendra … wedew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s