Philipus Silaen Philipus Silaen - 2 months ago 9
Android Question

Json Array Request with volley not works

I have Json value like this

{
"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
}


and then I want to retrieve it in my android app with volley like this

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


why it is not appear in my app. because it works before I add "events" array in my json value. so before I add events array my json value justlike this

[2]
0: {
"no": 1
"id": "17"
"judul": "Compfest 8 Seminar"
"deskripsi": "tes"
"duit": 47
"persen": 47
"sisahari": 47
}-
1: {
"no": 2
"id": "19"
"judul": "Grand Launching Gerakan UI Mengajar 5"
"deskripsi": "tes"
"duit": 80
"persen": 80
"sisahari": 80
}


and my code when it works like this :

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) {
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {


JSONObject obj = response.getJSONObject(i);
Cart news = new Cart();

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

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


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

Answer

Look at your json format when it work

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

it starts with array first so you can use jsonarrayrequest

but when you change your json format it starts with Json object not array again

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

So you can use jsonobjectrequest instead of jsonarrayrequest or the second option you just change your new json format with array first instead of object