在ListView Facebook API中显示JSONARRAY数据



我正在尝试在ListView中显示JsonArray用户数据,该数据来自Facebook Graph API响应,

这是我正在尝试的代码:

                    try
                    {
                        JSONObject resObject = response.getJSONObject();
                        JSONArray jsonArray = resObject.getJSONArray("data");

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                            if(jsonArray.getJSONObject(i).has("likes"))
                            {
                                JSONObject jsonObjectlikes = jsonObject.getJSONObject("likes");
                                JSONArray jsonArrayLikeData = jsonObjectlikes.getJSONArray("data");
                                for (int j = 0; j < jsonArrayLikeData.length(); j++) {
                                    JSONObject jsonObjectlike = jsonArrayLikeData.getJSONObject(j);
                                    String listid = jsonObjectlike.getString("id");
                                    String listname = jsonObjectlike.getString("name");
                                    HashMap<String, String> contact = new HashMap<>();
                                    contact.put("id", listid);
                                    contact.put("name", listname);
                                    contactList.add(contact);
                                }
                            }
                            ListAdapter adapter = new SimpleAdapter(
                                    MainActivity.this, contactList,
                                    R.layout.activity_listview, new String[]{"id", "name"}, new int[]{R.id.id,
                                    R.id.name});
                            simpleList.setAdapter(adapter);
                        }
                    }

这是JSON:

{
   "data": [
      {
         "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
         "likes": {
            "data": [
               {
                  "id": "xxxx",
                  "name": "xxx"
               }
            ],
         }
      }
   ]
}

上面的代码获取喜欢的数据,我想在简单的ListView中显示此数据。

尝试此

  1. 添加contactList=new ArrayList<>();

  2. simpereadapter apapter = new SimpereDeadapter( MainAttivity。此,联络清单, r.layout.activity_listview,new String [] {" id"," name"},new Int [] {r.id.id, r.id.name});

尝试此代码

 public void GetFacebookData() {   
contactList=new ArrayList<>();
Bundle parameters = new Bundle();
parameters.putString("fields", "likes");
parameters.putString("limit", "50");
new GraphRequest(
        AccessToken.getCurrentAccessToken(),
        "/me/posts",
        parameters,
        HttpMethod.GET,
        new GraphRequest.Callback() {
            public void onCompleted(GraphResponse response) {
                //Log.v("TAG", "Facebook Photos response: " + response);
                try
                {
                    JSONObject resObject = response.getJSONObject();
                    JSONArray jsonArray = resObject.getJSONArray("data");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        Log.i("ID", "-->" + jsonObject.getString("id"));
                        if(jsonArray.getJSONObject(i).has("likes"))
                        {
                            JSONObject jsonObjectlikes = jsonObject.getJSONObject("likes");
                            JSONArray jsonArrayLikeData = jsonObjectlikes.getJSONArray("data");
                            for (int j = 0; j < jsonArrayLikeData.length(); j++) {
                                JSONObject jsonObjectlike = jsonArrayLikeData.getJSONObject(j);
                                String listid = jsonObjectlike.getString("id");
                                String listname = jsonObjectlike.getString("name");
                                HashMap<String, String> contact = new HashMap<>();
                                contact.put("id", listid);
                                contact.put("name", listname);
                                contactList.add(contact);
                                Log.i("Like ID", "-->" + jsonObjectlike.getString("id"));
                                Log.i("Like Name", "-->" + jsonObjectlike.getString("name"));
                            }
                        }

                        SimpleAdapter  adapter = new SimpleAdapter(
                                MainActivity.this, contactList,
                                R.layout.activity_listview, new String[]{"id", "name"}, new int[]{R.id.id,
                                R.id.name});
                        simpleList.setAdapter(adapter);
                    }
                }
                catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).executeAsync();
}           

最新更新