简体   繁体   English

Android + Facebook SDK 3.0入门示例

[英]Android + Facebook SDK 3.0 Getting started exaple

I am developing an application and I want to integrate with facebook. 我正在开发应用程序,并且想与Facebook集成。 And I started with the first example ( first app facebook SDK 3.0 POINT 6 ) but it doesnt work and i dont know why. 我从第一个示例开始( 第一个应用程序facebook SDK 3.0 POINT 6 ),但是它不起作用,我也不知道为什么。

Here is Main Activity: 这是主要活动:

    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.content.pm.PackageManager.NameNotFoundException;
    import android.content.pm.Signature;
    import android.widget.TextView;
    import com.facebook.*;
    import com.facebook.model.*;

    import android.util.Base64;
    import android.util.Log;
    import android.view.Menu;

    public class MainActivity extends Activity {
    @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Add code to print out the key hash
        try {
            PackageInfo info = getPackageManager().getPackageInfo(
                    "com.facebook.login", 
                    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) {

        }



        // start Facebook Login
        Session.openActiveSession(this, true, new Session.StatusCallback() {

          // callback when session changes state
          @Override
          public void call(Session session, SessionState state, Exception exception) {
            if (session.isOpened()) {

              // make request to the /me API
              Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                // callback after Graph API response with user object
                @Override
                public void onCompleted(GraphUser user, Response response) {
                  if (user != null) {
                    TextView welcome = (TextView) findViewById(R.id.wellcome );
                    welcome.setText("Hello " + user.getName() + "!");
                  }
                }
              });
            }
          }
        });
      }

      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
      }
}

Here is the manifest: 这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.facebook.login"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.facebook.login.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.facebook.LoginActivity"/>
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>

    </application>

</manifest>

and here is the activity_main (in RelativeLayout): 这是activity_main(在RelativeLayout中):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

<TextView
    android:id="@+id/wellcome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

When i launch the activity appear this: appear a dialog. 当我启动活动时,出现以下内容:出现一个对话框。 It said this: 它说:

" Login would like to access your public profile and frien list (OK/CANCEL)" “登录后将访问您的公开个人资料和好友列表(确定/取消)”

and When I put ok appear this: " Hello Word" 当我确定时,出现以下内容:“ Hello Word”

The problem is not appear the user name (Hello Martin). 问题是没有出现用户名(Hello Martin)。

What is the problem ? 问题是什么 ?

Try to execute your application by setting the empty textview 尝试通过设置空的textview执行您的应用程序

android:text=""  

instead of 代替

android:text="@string/hello_world"

and after executing , if you get the empty textview again , there the problem might be in your method 在执行后,如果再次获得空的textview,则方法中可能存在问题

user.getName()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM