我正在尝试使用Facebook API来允许用户登录我的应用,登录操作成功完成并获得访问令牌但是当我使用GraphRequest获取用户的信息时它的响应和对象返回null ,我在Android开发中完全是新手。 这是我的代码:
public class SignIn extends Fragment {
private LoginButton loginButton;
private CallbackManager callbackManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_sign_in, container, false);
Button button = (Button) v.findViewById(R.id.button_signin);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getActivity(),Home.class);
startActivity(i);
getActivity().finish();
}
});
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) v.findViewById(R.id.facebook_button);
loginButton.setReadPermissions("email");
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
= try {
Log.d("id",object.getString("id")) ;
Log.d("email",object.getString("email")) ;
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity().getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
}
});
return v ;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
build.gradle(项目)(如果需要)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
答案 0 :(得分:1)
我想通了,我忘了把它添加到清单中:
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name" />