在listview android中显示JSONArray数据 - Facebook api

时间:2017-12-27 13:35:08

标签: android facebook listview

我试图在listview中显示来自facebook graph api response的JSONArray用户数据,

以下是我尝试的代码:

                    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"
               }
            ],
         }
      }
   ]
}

上面的代码获取了喜欢的数据,我想在简单的列表视图中显示这些数据。

1 个答案:

答案 0 :(得分:0)

试试这个

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

  2. SimpleAdapter适配器=新的SimpleAdapter(                                 MainActivity.this,contactList,                                 R.layout.activity_listview,new String [] {&#34; id&#34;,&#34; name&#34;},new int [] {R.id.id,                                 R.id.name});

  3. 试试此代码

     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();
    }