Android - Volley错误回复

时间:2015-02-17 03:51:38

标签: android json android-volley

尝试解析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"
    }]
}

代码

// 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"));
                                user.setProfilePhotoUrl(obj.getString("profilePicture"));
                                user.setLastActive(obj.getString("lastLogin"));

                                // adding movie to movies 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();

                    }
                });

请指教。谢谢。

3 个答案:

答案 0 :(得分:5)

注意:

=&gt; 如果字符串包含{第一个字符,则字符串包含JSONObject作为根容器

=&gt; 如果字符串包含[第一个字符,则字符串包含JSONArray作为根容器

发布json字符串包含JSONObject作为root而不是JSONArray。使用JsonObjectRequest代替JsonArrayRequest使用Volley向服务器发出请求

答案 1 :(得分:2)

您在对象内的数组中拥有用户对象。所以你想要从new JsonObjectRequest开始。

编辑:使用下面的代码,您可以检索JSONObject类型response,在其中有一个JSONArray类型users,之后您可以遍历JSONArray并从中检索每个JSONObject。

JsonObjectRequest req = new JsonObjectRequest(URL, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        JSONArray users = response.getJSONArray("users");
                        for (int i = 0; i < users.length(); i++) {
                          try {

                              JSONObject obj = users.getJSONObject(i);
                              User user = new User();
                              user.setName(obj.getString("name"));
                              user.setProfilePhotoUrl(obj.getString("profilePicture"));
                              user.setLastActive(obj.getString("lastLogin"));

                              // adding movie to movies array
                              userList.add(user);

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

                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.e("Error: ", error.getMessage());
                    }
                });

答案 2 :(得分:-1)

MainActivity onErrorResponse

JsonArrayRequest jArr = new JsonArrayRequest(url_select, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Log.d(TAG, response.toString());

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

                    Data item = new Data();

                    item.setId(obj.getString(TAG_ID));
                    item.setNama(obj.getString(TAG_NAMA));
                    item.setAlamat(obj.getString(TAG_ALAMAT));

                    // menambah item ke array
                    itemList.add(item);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            // notifikasi adanya perubahan data pada adapter
            adapter.notifyDataSetChanged();

            swipe.setRefreshing(false);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            swipe.setRefreshing(false);
        }
    });