我尝试使用Facebook凭据记录我的应用程序。它运行时没有错误,但它没有显示用户名。
它加载了Facebook登录页面,但它没有在文本视图中显示Facebook名称。
我按照This指南行。
这是活动类。
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.*;
import com.facebook.model.*;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 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.welcome);
welcome.setText("Hello " + user.getName() + "!");
// Toast.makeText(getApplicationContext(), user.getName(), Toast.LENGTH_LONG);
Log.d("User Name", user.getName());
}
}
});
}
else{
Toast.makeText(getApplicationContext(), "Error...", Toast.LENGTH_LONG);
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
}
我将这两行添加到proguard-android.txt以及
#-keep class com.facebook.** { *; }
#-keepattributes Signature
这是我使用的库
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
好吧,这是我的解决方案 -
首先将FacebookSDK导入您的工作区。
第二次将FacebookSDK添加到您的项目中。右键单击您的项目 - > gt; properties-> Android-> Library-> Add-> FacebookSDK->确定
第3次添加FacebookSDK.jar作为外部jar。右键单击你的项目 - > gt> properties-> Java Build path-> Library->添加外部jar-> [你的facebookSDK.jar文件的路径] - > ok
这里是MainActivity
public class MainActivity extends Activity {
Button btnLoginFB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLoginFB = (Button) findViewById(R.id.btnLoginFB);
btnLoginFB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// start Facebook Login
Session.openActiveSession(MainActivity.this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
Toast.makeText(MainActivity.this, user.getName(), Toast.LENGTH_SHORT).show();
}
}
}).executeAsync();
}
}
});
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
}
这是AndroidManmifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mysample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
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.example.mysample.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>
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
<activity android:name="com.facebook.LoginActivity" />
</application>
</manifest>
app_id是您在Facebook开发者帐户中创建的应用程序ID。
成功登录后,它会翻转用户名。