问题描述
您好我想实现的Facebook登录和获取好友列表。我储存我Friends'name和个人资料照片网址为的ArrayList< TaggedFriends>
其中TaggedFriends类将有set和get方法。
Hi I am trying to implement Facebook login and getting friends list. I am storing my Friends'name and profile picture url into ArrayList<TaggedFriends>
Where TaggedFriends class will have set and get methods.
我现在在一个名为MainFragment片段类的工作。
在这里,我存储值转换为FacebookCallback会话中taggableFriends变量。但我不能访问任何其他地方
我想将它保存到共享preferences但是当我尝试存储,它返回空值。
I am now working in Fragment class named MainFragment.Here, I am storing values into taggableFriends variable inside a FacebookCallback Session. But I can't access it to any other placesI am trying to store it into SharedPreferences but when I try to store, it returning null value.
下面是MainFragment我满code
Here is my full code of 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 - > 的onSuccess ()方法。有我在我的存储值变量 taggedFriends
Please notice inside FacebookCallBack --> onSuccess() method. There I am storing my values for variable taggedFriends
紧接着我储存的 taggedFriends 到共享preferences。但是,在那个地方, taggedFriends 变量为空。它不包含任何内容。我logcat的检查。
Immediately after that I am storing taggedFriends into SharedPreferences. But at that place, taggedFriends variable was null. It doesn't contain anything. I checked with Logcat.
里面的onSuccess()方法,它的价值在那里,当我使用了这个方法,它的空。有什么问题?
Inside onSuccess() method its values are there, when I use out of that method, it's null. What is the problem?
推荐答案
在图形API调用是在一个单独的线程异步执行,即。看起来,当您尝试和值存储在共享preferences,异步调用尚未完成,你会得到空。尝试移动在共享preferences一部分存储的onCompleted方法里面,你把它添加到您的ArrayList之后。
The Graph API call is executed asynchronously, i.e. in a separate thread. Looks like when you try and store the values in SharedPreferences, the async call hasn't completed yet and you get null. Try moving the storing in SharedPreferences part inside the onCompleted method, right after you add it to your ArrayList.
这篇关于为什么我不能用FacebookCallback超出了我的ArrayList变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!