带有齐射的Json阵列请求不起作用

时间:2016-09-17 06:20:56

标签: android arrays json android-volley

我有像这样的Json值

template <typename T>
constexpr std::add_const_t<T>& as_const(T& t) noexcept{ return t; }

然后我想在我的Android应用程序中使用这样的排球检索它

if (parts.length > 1) {
    ... // what to do if the string contains the delimiter
}
else {
    ... // what to do if the string does not contain the delimiter
}

为什么它没有出现在我的应用中。因为它在我添加&#34;事件&#34;之前有效。我的json值中的数组。所以在我添加事件数组之前我的json值就像这个

{
"events": [2]
0:  {
"no": 1
"id": "2"
"nama": "Meja dengan kaki kuda"
"harga": 700000
"gambar": "poster/Donor_darah.jpg"
"stok": 39
"qty": 3
"status": 0
"total": 2100000
}-
1:  {
"no": 2
"id": "1"
"nama": "Lemari"
"harga": 500000
"gambar": "poster/grand-launching-gerakan-ui-mengajar-51.png"
"stok": 0
"qty": 4
"status": 0
"total": 2000000
}-
-
"total": 4100000
}

和我的代码一样,如下所示:

JsonArrayRequest arrReq = new JsonArrayRequest(Request.Method.POST, DATA_GET_NOTIF + page,null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());

                        if (response.length() > 0) {
                            try {
                                for (int a = 0; a < response.length(); a++) {
                                    JSONObject obj = response.getJSONObject(a);
                                    JSONArray event = obj.getJSONArray("events");
                                    Log.d("JsonArray",response.toString());

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

                                        JSONObject jb = (JSONObject) event.get(i);
                                        Cart news = new Cart();

                                        int total = 0;
                                        no = jb.getInt("no");

                                        news.setId(jb.getString("id"));
                                        news.setJudul(jb.getString("nama"));
                                        news.setHarga(jb.getInt("harga"));
                                        news.setStok(jb.getInt("stok"));
                                        news.setQty(jb.getInt("qty"));
                                        news.setStatus(jb.getInt("status"));
                                        news.setTotal(jb.getInt("total"));


                                        if (jb.getString("gambar") != "") {
                                            news.setImageUrl(jb.getString("gambar"));
                                        }


                                        // adding news to news array
                                        eventList.add(news);

                                        if (no > offSet)
                                            offSet = no;

                                        Log.d(TAG, "offSet " + offSet);


                                        // notifying list adapter about data changes
                                        // so that it renders the list view with updated data
                                        adapter.notifyDataSetChanged();
                                    }
                                }
                            }catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        swipe.setRefreshing(false);
                    }

                }, new Response.ErrorListener() {

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

        }){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("authorization", apikeys);
            return params;
        }};

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(arrReq);

1 个答案:

答案 0 :(得分:1)

查看你的json格式

**[2]**
0:  {
"no": 1
"id": "17"
"judul": "Compfest 8 Seminar"
"deskripsi": "tes"
"duit": 47
"persen": 47
"sisahari": 47
}-

首先从数组开始,这样你就可以使用jsonarrayrequest

但是当你改变你的json格式时,它会以Json对象而不是数组再次开始

**{**
"events": [2]
0:  {
"no": 1
"id": "2"
"nama": "M

所以你可以使用jsonobjectrequest代替jsonarrayrequest或第二个选项你只需要先用数组而不是对象改变你的新json格式