Global variable lose value after GraphRequest of Facebook - android

I've an issue with an arraylist used inside Facebook's code. public class Facebook {

//this are global variable
HashMap<String,String> postsMap = new HashMap<>();
ArrayList<HashMap<String, String>> list = new ArrayList<>();
...
private void retrievePosts() {
GraphRequest postsRequest = new GraphRequest(
            AccessToken.getCurrentAccessToken(),
            "/{page-id}/",
            null,
            HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    Log.d("Facebook: ", response.toString());

                    try {
                        JSONObject jobj = response.getJSONObject().getJSONObject(TAG_POST);
                        JSONArray posts = jobj.getJSONArray(TAG_DATA);

                        for (int i = 0; i < posts.length(); i++) {
                            JSONObject c = posts.getJSONObject(i);

                            String message = c.getString(TAG_MESSAGE);
                            String createdTime = c.getString(TAG_CREATION);

                            map.put(TAG_MESSAGE, message);
                            map.put(TAG_CREATION, createdTime);


                            list.add(postsMap);

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "posts.limit(20)");
    postsRequest.setParameters(parameters);
    postsRequest.executeAsync();
}

When i try to see what is inside the list inside the Facebook method call i can see the right elements, but when the method end my arraylist become empty. How can I do to resolve this issue?

Answers


If I'm not mistaken, your HashMap should be initialized with

HashMap<String,String> postsMap = new HashMap<String,String>();

Then you should be using your postsMap variable in your calls to put()

postsMap.put(TAG_MESSAGE, message);
postsMap.put(TAG_CREATION, createdTime); 

I found a resolution for my problem. The problem was that i was executing an asyncTask in the main thread. i resolved in this way:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view);

    new RetrievePosts().execute();

}
class RetrievePosts extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
         GraphRequest postsRequest = new GraphRequest(
        AccessToken.getCurrentAccessToken(),
        "/{page-id}/",
        null,
        HttpMethod.GET,
        new GraphRequest.Callback() {
            public void onCompleted(GraphResponse response) {
                Log.d("Facebook: ", response.toString());
                //my stuff
            }
        });
Bundle parameters = new Bundle();
parameters.putString("fields", "posts.limit(20)");
postsRequest.setParameters(parameters);
postsRequest.executeAndWait();
}

I had changed .executeAsynk() with method .executeAndWait() because i'm already in asyncTask.


Need Your Help

Backend-server for mobile apps

android ios mobile web backend

I was wondering how people generally realize backends for either complex data-synchronization ("cloud"-sync, since everyone seems to love that word) or simple user-management when developing mobile...