Android - 在片段(列表视图)中解析复杂的 json

Android - Parse complex json in fragment (listview)

如何解析片段(列表视图)中复杂的 json 数据。我无法在片段中获取所有 json 数据。

错误

Json数据

{
    "users": [{
        "userId": 1,
        "name": "Dya Vega",
        "profilePhoto": "https://graph.facebook.com/1301454197/picture?type=large",
        "dateMatched": "1/1/2015",
        "distance": "1 miles away",
        "status": "Online",
        "requestMessage": "Hi, can I know you?",
        "like": 234,
        "lastActive": "Active 1 hour ago"
    }, {
        "userId": 2,
        "name": "Esa Ezzatinor",
        "profilePhoto": "https://graph.facebook.com/1269334432/picture?type=large",
        "dateMatched": "1/1/2015",
        "distance": "2 miles away",
        "status": "Online",
        "requestMessage": "Hi, can I know you?",
        "like": 234,
        "lastActive": "Active 2 hour ago"
    }]
}

HistoryFragment.java

public class HistoryFragment extends Fragment {

...

// Creating volley request obj
        JsonArrayRequest userReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                User user = new User();
                                User.setName(obj.getString("name")); // error here
                                User.setThumbnailUrl(obj.getString("profilePicture")); // error here
                                User.setLastLogin(obj.getString("lastLogin")); // and here

                                // adding user to users array
                                userList.add(user);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        hidePDialog();

                    }
                });

...

}

User.java (Model/Object)

public class User {
    private String name, lastActive, profilePhotoUrl;

    public User() {
    }

    public User(String name, String lastActive, String profilePhotoUrl) {
        this.name = name;
        this.lastActive = lastActive;
        this.profilePhotoUrl = profilePhotoUrl;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastActive() {
        return lastActive;
    }

    public void setLastActive(String lastActive) {
        this.lastActive = lastActive;
    }

    public String getProfilePhotoUrl() {
        return profilePhotoUrl;
    }

    public void setProfilePhotoUrl(String profilePhotoUrl) {
        this.profilePhotoUrl = profilePhotoUrl;
    }
}

你应该将 methods 设置为 User class Object like

  User user = new User();
  user.setName(obj.getString("name")); 
  user.setThumbnailUrl(obj.getString("profilePicture")); 
  user.setLastLogin(obj.getString("lastLogin")); 

您只能通过创建 class 的对象来访问所有用户 class 方法。

User class 所有方法都是 non-static 但尝试使用 class 名称访问所有方法( 静态方法仅使用 class名字)。

创建 class 对象以访问所有方法

User user = new User();

现在使用 user 访问用户 class 的所有 getter/setter 方法。

如果想访问所有方法而不像当前那样使用 class 名称创建对象,则在用户 class 中为所有方法添加静态。