0

I am trying to obtain all the id, name and image of all friends that have my app installed. When I debug the graph request, the JSONArray is populated with the correct data. But I am not sure how to properly get the data out of the request.

This is my request that does create the correct JSONArray:

GraphRequest friendsRetrievalRequest = GraphRequest.newMyFriendsRequest(
                    AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONArrayCallback() {
                        @Override
                        public void onCompleted(JSONArray jsonArray, GraphResponse response) {                                
                            try {
                                JSONObject jsonObject = response.getJSONObject();        
                                JSONObject summary = jsonObject.getJSONObject("summary");                            
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });

I have tried:

            Bundle params = new Bundle();
            params.putString("fields", "id,name,picture");
            friendsRetrievalRequest.setParameters(params);
            friendsRetrievalRequest.executeAsync();

            Intent intent = new Intent(getApplicationContext(), ContactsListActivity.class);
            intent.putExtras(params);
            startActivity(intent);

But in my ContactsListActivity class I call this:

    Bundle inBundle = getIntent().getExtras();
    String name = inBundle.get("id").toString();
    String surname = inBundle.get("name").toString();
    String imageUrl = inBundle.get("picture").toString();

But is all null.

Where am I going wrong? How can I retrieve the array of the friend data obtained within the Graph Request?

EDIT: This is the JSON array I receive, there is one friend which is as expected (I have removed their personal data for privacy, but it is all correct):

[{"id":"their id","name":"their name","picture":{"data":{"is_silhouette":false,"url":"their image url"}}}]
Calco
  • 1,420
  • 1
  • 17
  • 31
  • post your json response – Pavneet_Singh Dec 08 '16 at 14:36
  • I have added the JSON response, and as you can see it a single result JSON array as expected – Calco Dec 08 '16 at 16:13
  • where you are actually parsing this json response because there is no `summary` when you doing this `jsonObject.getJSONObject("summary")` , add your json parsing code – Pavneet_Singh Dec 08 '16 at 16:16
  • So if Bundle take params "id, name, picture", do I have to define these somewhere? How does Bundle actually receive the JSON array data? – Calco Dec 08 '16 at 16:24
  • simply you have to parse your json object and mean fetch your string and int etc and pass it to next activity putString etc methods , take a look at this answer and question to parse json example http://stackoverflow.com/questions/5015844/parsing-json-object-in-java/40828056#40828056 – Pavneet_Singh Dec 08 '16 at 17:04
  • I understand how to parse JSON. But what am I parsing it into? Where is the data going? You say pass it to the next activities putString, but putString is in the same activity? Would you be able to provide an example in an answer as I am not following what you mean. – Calco Dec 08 '16 at 17:11
  • e.g `String name= jsonobject.optString("name");` where `name` is string object and `jsobobject` is a demo object and later you do `bundle.putString("anykey",name);` – Pavneet_Singh Dec 08 '16 at 17:18
  • But if it is a list of friends, I'll have a single defined String name. How would I then store >1 friends? – Calco Dec 08 '16 at 17:23
  • Also is that not what I am already doing in my above code, the second snippet? – Calco Dec 08 '16 at 17:25
  • that is just a demo , to create a list of friends follow the link i gave you , read it carefully but that is just for string , you can create a list of POJO object (mean your class) and traverse all friends (json array) and store it into list – Pavneet_Singh Dec 08 '16 at 17:33
  • Are you saying call start the next activity from within the graph request? Because I have added "String id = jsonArray.getJSONObject(0).optString("id");" within the graph request, which does pull the correct id String. But now what? I've already got "putString" for the bundle outside of the graph request shown above? I have the data, I understand how to parse it. But how do I get it out of the async graph request! – Calco Dec 08 '16 at 17:51

0 Answers0