您好我正在尝试实施Facebook登录并获取好友列表。我将我的好友&#39;名称和个人资料图片网址存储到ArrayList<TaggedFriends>
TaggedFriends类将设置并获取方法。
我现在在名为MainFragment的Fragment类中工作。 在这里,我将值存储在FacebookCallback Session中的taggableFriends变量中。但我无法访问任何其他地方 我试图将它存储到SharedPreferences但是当我尝试存储时,它返回null值。
这是我的MainFragment的完整代码
public class MainFragment extends Fragment {
String email, tag = "MainFragment kbt";
ArrayList<TaggableFriends> taggableFriends = new ArrayList<TaggableFriends>();
ArrayList<TaggableFriends> testing = new ArrayList<>();
MainActivity mainActivity ;
private CallbackManager callbackManager;
private TextView textView;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/taggable_friends",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
// handle the result
Log.d("RESPONSE KBT", response.getRawResponse());
try {
JSONObject obj = new JSONObject(response.getRawResponse());
JSONArray array = obj.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject indi = array.getJSONObject(i);
JSONObject pic = indi.getJSONObject("picture");
JSONObject data = pic.getJSONObject("data");
String name = indi.getString("name");
String url = data.getString("url");
TaggableFriends tf = new TaggableFriends(name, url);
taggableFriends.add(tf);
Log.d("NAME", tf.getName());
Log.d("SIZE", ":"+taggableFriends.size());
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.d(tag,"SECOND SIZE"+taggableFriends.size());
//
}
}
).executeAsync();
JSONArray mJSONArray = new JSONArray(taggableFriends);
SharedPreferences mPrefs = getActivity().getSharedPreferences("My_File",Context.MODE_PRIVATE);
SharedPreferences.Editor mEditor = mPrefs.edit();
mEditor.putString("myKey", mJSONArray.toString());
Log.d(tag,"STRING JSON : "+mJSONArray.toString());
mEditor.commit();
displayMessage(profile);
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
displayMessage(newProfile);
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
textView = (TextView) view.findViewById(R.id.textView);
// loginButton.setReadPermissions("user_friends");
loginButton.setReadPermissions(Arrays.asList("email", "user_friends", "read_custom_friendlists"));
// loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, callback);
Log.d(tag,"taggable "+taggableFriends.size());
// if(taggableFriends.size()!=0)
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
private void displayMessage(Profile profile) {
if (profile != null) {
textView.setText(profile.getName());
}
}
}
请注意 FacebookCallBack - &gt; onSuccess ()方法。我在那里存储变量 taggedFriends
的值之后我立即将 taggedFriends 存储到SharedPreferences中。但在那个地方, taggedFriends 变量为null。它不包含任何东西。我用Logcat检查过。
在onSuccess()方法中,它的值在那里,当我使用该方法时,它是null。有什么问题?
答案 0 :(得分:2)
Graph API调用是异步执行的,即在单独的线程中执行。看起来当您尝试将值存储在SharedPreferences中时,异步调用尚未完成,您将获得null。在将其添加到ArrayList后立即尝试在onCompleted方法内的SharedPreferences部分中移动存储。