1
我試圖獲得在Facebook Android SDK中的用戶信息..不是從<code>Request.newMeRequest(session, new Request.GraphUserCallback()</code>這是從Android SDK中經理更新的功能,工作
我沒有得到結果的Facebook的Android代碼
public class MainFragment extends Fragment {
private static final String TAG = "MainFragment";
private UiLifecycleHelper uiHelper;
private TextView userInfoTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main, container, false);
LoginButton authButton = (LoginButton) view
.findViewById(R.id.authButton);
authButton.setFragment(this);
authButton.setReadPermissions(Arrays.asList("user_location",
"user_birthday", "user_likes"));
userInfoTextView = (TextView) view.findViewById(R.id.userInfoTextView);
return view;
}
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (state.isOpened()) {
userInfoTextView.setVisibility(View.VISIBLE);
Settings.addLoggingBehavior(LoggingBehavior.REQUESTS);
Request.newMeRequest(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
// TODO Auto-generatedation I put the tags below in wrong
// places (I method stub
if (user != null) {
// Display the parsed user info
Toast.makeText(null, "Entered the user section",
Toast.LENGTH_LONG).show();
userInfoTextView.setText(buildUserInfoDisplay(user));
} else if (user == null) {
Toast.makeText(null, "User is null error in request",
Toast.LENGTH_LONG).show();
}
}
}).executeAsync();
Log.i(TAG, "Logged in...");
} else if (state.isClosed()) {
userInfoTextView.setVisibility(View.INVISIBLE);
Log.i(TAG, "Logged out...");
}
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if (session != null && (session.isOpened() || session.isClosed())) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@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);
}
private String buildUserInfoDisplay(GraphUser user) {
StringBuilder userInfo = new StringBuilder("");
userInfo.append(String.format("Name: %s\n\n", user.getName()));
userInfo.append(String.format("Birthday: %s\n\n", user.getBirthday()));
userInfo.append(String.format("Location: %s\n\n", user.getLocation()
.getProperty("name")));
userInfo.append(String.format("Locale: %s\n\n",
user.getProperty("locale")));
// JSONArray languages = (JSONArray)user.getProperty("languages");
GraphObjectList<MyGraphLanguage> languages = (user
.cast(MyGraphUser.class)).getLanguages();
if (languages.size() > 0) {
ArrayList<String> languageNames = new ArrayList<String>();
for (MyGraphLanguage language : languages) {
languageNames.add(language.getName());
}
userInfo.append(String.format("Languages: %s\n\n",
languageNames.toString()));
}
return userInfo.toString();
}
private interface MyGraphLanguage extends GraphObject {
// Getter for the ID field
String getId();
// Getter for the Name field
String getName();
}
private interface MyGraphUser extends GraphUser {
// Create a setter to enable easy extraction of the languages field
GraphObjectList<MyGraphLanguage> getLanguages();
}
}