无法在android中导入Session,Request,Response,GraphUser包

时间:2015-08-05 06:14:44

标签: android facebook

我在ecllipe中导入了最新的facebook sdk。当我试图在我的应用程序中获取基本信息时,它不会导入各自的Request,Response和Session包。我会做什么?导入相应的包。 我尝试了下面的代码,但在导入相应的软件包时面临问题。

LoginActivity.java

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Session.getActiveSession() == null
                || Session.getActiveSession().isClosed()) {
            Session.openActiveSession(this, true, new StatusCallback() {

                @Override
                public void call(Session session, SessionState state,
                        Exception exception) {
                    System.out.println("State= " + state);

                    if (session.isOpened()) {
                        System.out.println("Token=" + session.getAccessToken());
                        Request.executeMeRequestAsync(session,
                                new GraphUserCallback() {
                                    @Override
                                    public void onCompleted(GraphUser user,
                                            Response response) {
                                        if (user != null) {
                                            System.out.println("User=" + user);

                                        }
                                        if (response != null) {
                                            System.out.println("Response="
                                                    + response);
                                            Toast.makeText(MainActivity.this,
                                                    response.toString(),
                                                    Toast.LENGTH_LONG).show();
                                        }
                                    }
                                });
                    }
                    if (exception != null) {
                        System.out.println("Some thing bad happened!");
                        exception.printStackTrace();
                    }
                }
            });
        }
        }

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

1 个答案:

答案 0 :(得分:3)

它发生在您身上,因为会话已从新的Android SDK 4中删除。因此您无法使用此代码获取用户信息。

尝试使用此代码。这对我来说很成功。

首先在onCreate中初始化FacebookSdk,然后再选择布局。

FacebookSdk.sdkInitialize(this.getApplicationContext());

loginButton = (LoginButton) findViewById(R.id.login_button);

List < String > permissionNeeds = Arrays.asList("user_photos", "email",
	"user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {@Override
	public void onSuccess(LoginResult loginResult) {

		System.out.println("onSuccess");

		String accessToken = loginResult.getAccessToken()
			.getToken();
		Log.i("accessToken", accessToken);

		GraphRequest request = GraphRequest.newMeRequest(
		loginResult.getAccessToken(),
		new GraphRequest.GraphJSONObjectCallback() {@Override
			public void onCompleted(JSONObject object,
			GraphResponse response) {
				Log.i("LoginActivity", response.toString());
				try {
					id = object.getString("id");
					try {
						URL profile_pic = new URL(
							"http://graph.facebook.com/" + id + "/picture?type=large");
						Log.i("profile_pic",
						profile_pic + "");

					} catch (MalformedURLException e) {
						e.printStackTrace();
					}
					String name = object.getString("name");
					String email = object.getString("email");
					String gender = object.getString("gender");
					String birthday = object.getString("birthday");
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
		Bundle parameters = new Bundle();
		parameters.putString("fields",
			"id,name,email,gender, birthday");
		request.setParameters(parameters);
		request.executeAsync();
	}

	@Override
	public void onCancel() {
		System.out.println("onCancel");
	}

	@Override
	public void onError(FacebookException exception) {
		System.out.println("onError");
		Log.v("LoginActivity", exception.getCause().toString());
	}
});

添加方法。

@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent data) {
	super.onActivityResult(requestCode, responseCode, data);
	callbackManager.onActivityResult(requestCode, responseCode, data);
}