Facebook android sdk fetch_user_data示例 - 无法获取数据

时间:2013-11-28 12:18:02

标签: android facebook facebook-graph-api facebook-android-sdk

我正在尝试通过developers.facebook.com

获取此处Fetch_user_example给出的示例中的用户数据

当我按下登录时,webview会打开,我可以登录,但之后它会重定向到同一页面而不会退出链接

以及我所做的图表Api请求没有显示在登录按钮下面(我添加了教程中给出的隐藏文本视图)

这是代码

public class MainFragment extends Fragment{

private static final String TAG = "MainFragment";
private UiLifecycleHelper uiHelper;

private TextView userInfoTextView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);
    Settings.addLoggingBehavior(LoggingBehavior.REQUESTS);
}

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (session != null && session.isOpened()) {
        // Get the user's data.
        userInfoTextView.setVisibility(View.VISIBLE);

        // Request user data and show the results
        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) {
                // Display the parsed user info
                userInfoTextView.setText(buildUserInfoDisplay(user));
            }
          }
        }).executeAsync();

    }else if(session.isOpened()){
        userInfoTextView.setVisibility(View.INVISIBLE);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, 
        ViewGroup container, 
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fblogin, container, false);
    LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
    authButton.setFragment(this);
    authButton.setReadPermissions(Arrays.asList("user_likes", "user_status","user_location","email","user_birthday"));

    // Check for an open session
    Session session = Session.getActiveSession();
    if (session != null && session.isOpened()) {
        // Get the user's data

    }
    userInfoTextView = (TextView) view.findViewById(R.id.userInfoTextView);

    return view;

}

private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

@Override
public void onResume() {
    super.onResume();
    // For scenarios where the main activity is launched and user
    // session is not null, the session state change notification
    // may not be triggered. Trigger it if it's open/closed.
    Session session = Session.getActiveSession();
    if (session != null &&
           (session.isOpened() || session.isClosed()) ) {
        onSessionStateChange(session, session.getState(), null);
    }

    uiHelper.onResume();
}

private String buildUserInfoDisplay(GraphUser user) {
    StringBuilder userInfo = new StringBuilder("");

    // Example: typed access (name)
    // - no special permissions required
    userInfo.append(String.format("Name: %s\n\n", 
        user.getName()));

    // Example: typed access (birthday)
    // - requires user_birthday permission
    userInfo.append(String.format("Birthday: %s\n\n", 
        user.getBirthday()));

    // Example: partially typed access, to location field,
    // name key (location)
    // - requires user_location permission
    userInfo.append(String.format("Location: %s\n\n", 
        user.getLocation().getProperty("name")));

    // Example: access via property name (locale)
    // - no special permissions required
    userInfo.append(String.format("Locale: %s\n\n", 
        user.getProperty("locale")));

    // Example: access via key for array (languages) 
    // - requires user_likes permission
    JSONArray languages = (JSONArray)user.getProperty("languages");
    if (languages.length() > 0) {
        ArrayList<String> languageNames = new ArrayList<String> ();
        for (int i=0; i < languages.length(); i++) {
            JSONObject language = languages.optJSONObject(i);
            // Add the language name to a list. Use JSON
            // methods to get access to the name field. 
            languageNames.add(language.optString("name"));
        }           
        userInfo.append(String.format("Languages: %s\n\n", 
        languageNames.toString()));
    }

    return userInfo.toString();
}

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

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}
 }

无法弄清楚是什么问题。 等待有用的回复

感谢。

1 个答案:

答案 0 :(得分:2)

我通过检查onCreateView

中的status.isOpened和isClosed来解决它

更改如下:

private void onSessionStateChange(Session session, SessionState state, Exception exception) {


if (state.isOpened()) {
           Log.i(TAG, "Logged in...");
        userInfoTextView.setVisibility(View.VISIBLE);

        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) {
                // Display the parsed user info
                userInfoTextView.setText(buildUserInfoDisplay(user));
                Log.d(TAG,"Failed to login");
            }
          }
        }).executeAsync();

    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
        userInfoTextView.setVisibility(View.INVISIBLE);
    }
}