19
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[超簡単]Simple android facebookを使ってみた!

Posted at

はじめに

Facebookを利用したAppを作るのに欠かせないFacebookSDK
コレを凄く簡単に使えるsimple android facebook というライブラリを使ってfacebookの友達情報を取得したのででメモります。

ちなみに本家がむっちゃ詳しいwikiがあるので詳しいこととかはそっちを読んだ方が早いです。

本家 simple android facebook

感想

まじ簡単。
友達情報を取得したいとか、投稿したいとかならFacebookSDKを使うよりもこっちの方が良いと思います。

ただ、今回本当は使いたかった自分の所属している会社のemployeeみたいなquerは投げられませんでした。
もともと用意してくれているmethodが部分的で、全部のfacebook sdkの機能が使える訳ではないので導入前に確認がwikiで確認が必要かと思います。

やっぱ本家が必要なのか。。。

もしこれがただの自分の理解不足で「所属groupのmember」とか「所属companyのemployee」みたいなqueryをコレを使って投げられるって人が居たら教えて欲しいです。

導入

[1]本家 facebook sdk の import をします。

[2]simple android facebook も import します。

[3]simple android facebook に 本家 facebook sdk をライブラリとして追加

[4] 使いたいappにsimple android facebook を追加します

[5] string xmlに<string name="app_id">625994234086470</string>を追加します

[6]manifest xml に以下を追加

<uses-permission android:name="android.permission.INTERNET" />

<activity
    android:name="com.facebook.LoginActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

<meta-data
    android:name="com.facebook.sdk.ApplicationId"
    android:value="@string/app_id" />

[7]Activityの中で色々設定 & instance の設定

//onCreateの中

Permissions[] permissions = new Permissions[] {
    Permissions.USER_PHOTOS,
    Permissions.EMAIL,
    Permissions.PUBLISH_ACTION
};
SimpleFacebookConfiguration configuration = new SimpleFacebookConfiguration.Builder()
    .setAppId("625994234086470")
    .setNamespace("sromkuapp")
    .setPermissions(permissions)
    .build();
SimpleFacebook.setConfiguration(configuration);

//oncreateの外
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    mSimpleFacebook.onActivityResult(this, requestCode, resultCode, data); 
    super.onActivityResult(requestCode, resultCode, data);
} 

使い方

まずはLogin

//loginlistner を追加して
OnLoginListener onLoginListener = new OnLoginListener() {
    @Override
    public void onLogin() {
	//login後の処理記述
        Log.i(TAG, "Logged in");
    }

    @Override
    public void onNotAcceptingPermissions(Permission.Type type) {
        // user didn't accept READ or WRITE permission
        Log.w(TAG, String.format("You didn't accept %s permissions", type.name()));
    }

};

//これで呼び出す
mSimpleFacebook.login(onLoginListener);

取得したいものののリストを設定

properties = new Properties.Builder()
		.add(Properties.ID)
		.add(Properties.NAME)
		.add(Properties.LOCATION)
		.add(Properties.EDUCATION)
		.add(Properties.PICTURE, pictureAttributes)
		.build();

友達の取得

今回は後々扱いやすいようにJsonArrayとして友達のID、名前、学校名、住所、icon urlを取得する



OnFriendsListener onFriendsListener = new OnFriendsListener() {         
		@Override
		public void onComplete(List<Profile> friends) {
			for (int i = 0; i < friends.size(); i++) {
				try {
					JSONObject data = new JSONObject();
					data.put("id", friends.get(i).getId());
					data.put("name", friends.get(i).getName());
					data.put("icon_url", friends.get(i).getPicture());
					try {
						Log.e("school", friends.get(i).getEducation().get(0).getSchool());
						data.put("school_name", friends.get(i).getEducation().get(0).getSchool());						
					} catch (Exception e) {
						data.put("school_name","");
					}
					try {
						data.put("location_name", friends.get(i).getLocation().getName());
					} catch (Exception e) {
						data.put("location_name", "");
					}
					friendJsonArray.put(data);
				} catch (Exception e) {
					// TODO: handle exception
				}

			}
			Log.e("JsonData", friendJsonArray.toString());
			Log.i("FrindList", "Number of friends = " + friends.size());
			
		      // Editor の設定  
		      editor = pref.edit();  
		      // Editor に値を代入  
		      editor.putString(  
		          KEY_TEXT,
		          friendJsonArray.toString()
		      );  
		      // データの保存  
		      editor.commit();  
		      mSimpleFacebook.logout(onLogoutListener);
		}
		@Override
		public void onFail(String reason) {
			// TODO Auto-generated method stub	
		}
	};


//これで呼び出せる
mSimpleFacebook.getFriends(properties, onFriendsListener);
19
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?