Facebook的Andr​​oid SDK中fetch_user_data难道不会例子获取数据例子、数据、Andr、Facebook

2023-09-07 02:42:19 作者:时光,请慢些走

我想从这里举例给出 Fetch_user_example 按developers.facebook.com

I am trying to get user data from example given here Fetch_user_example by developers.facebook.com

之后,它重定向到相同的页面而不注销链接

When I press login then the webview opens and i can login but after that it redirect to same page without logout link

也是图形API请求我已经低于登录按钮没有显示出来(我已经添加在本教程中给出的隐藏文本视图)

and also the graph Api request i have made is not displayed below that Login Button(I have added the hidden text view as given in the tutorial)

下面是code

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);
}
 }

也弄不清是什么问题。等待回复有益

can't figure out what is the problem. Waiting for helpful reply

感谢。

推荐答案

我解决它在onCreateView检查status.isOpened和isClosed返

I solved it by checking status.isOpened and isClosed in onCreateView

变化如下:

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);
    }
}
 
精彩推荐
图片推荐