2

I try to parsing data from my database and show it on listview. However since in logcat does't show what error volley does, I don't know how to solve it. Here's my code:

JsonResult

{"result":[{"namaBarang":"kabel","jumlahBarang":"5","tglKel":"2018-06-06"},{"namaBarang":"optical power meter","jumlahBarang":"5","tglKel":"0000-00-00"}]}

From that json result, I try to parse it with JsonObject, and here's what my JsonObject looks like.

Activity

   JsonObjectRequest bkRequest=new JsonObjectRequest(Request.Method.GET, >url, null ,new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONObject obj = response.getJSONObject("result");
                BarangKeluar bk = new BarangKeluar();
                bk.setNamaBarang(obj.getString("namaBarang"));
                bk.setJumlahBarang(obj.getString("jumlahBarang"));
                bk.setTglBarang(obj.getString("tglBarang"));
                bkList.add(bk) ;
            } 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());
        }
    });
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(bkRequest);

But after I push it on my phone, the listview not populate with my json.

UPDATE1 Already trying to try pistolcaffe and IntelliJ Amiya code but still not working.

Logcat

D/TextView: setTypeface with style : 0 I/System.out: (HTTPLog)-Static: isSBSettingEnabled false I/System.out: (HTTPLog)-Static: isSBSettingEnabled false D/AbsListView: onsize change D/Volley: [1] 2.onErrorResponse: AppController

But my AppController work fine with This this tutorial.

Marked threads duplicate in Another threads. I look up on this and the difference is most of answer use HttpConnection.


Solved

Finally, I fixed this.

Here's the code after fix.

Activity

//Create JsonObjectRequest
JsonObjectRequest bkRequest = new JsonObjectRequest(Request.Method.GET, url, null,new
Response.Listener<JSONObject>(){
        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, response.toString());

            try {
                JSONArray obj = response.getJSONArray("result");
                for(int i=0;i< obj.length();i++) {
                    JSONObject json     = obj.getJSONObject(i);
                    BarangKeluar bk = new BarangKeluar();
                    bk.setNamaBarang(json.getString("namaBarang"));
                    bk.setJumlahBarang(json.getString("jumlahBarang"));
                    bk.setTglBarang(json.getString("tglKel"));

                    //Adding data into array
                    bkList.add(bk);
                }

            } catch (JSONException e) {
                e.printStackTrace();
                e.getMessage();
            }
            // 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());
        }
    });
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(bkRequest);
Cœur
  • 37,241
  • 25
  • 195
  • 267
tupir
  • 43
  • 5
  • Hey @Nilesh Rathod , i do lookup on similar thread, and most of the answer given were httpcon not a volley – tupir Jul 02 '18 at 03:24

2 Answers2

1

Expected Error is

JSONArray cannot be converted to JSONObject

  • Your result is JSONArray.
  • No tglBarang KEY present. Set proper name.

Don't

JSONObject obj = response.getJSONObject("result");

Do

 JSONArray jsonArray = response.getJSONArray("result");

RECTIFIED CODE

JSONArray jsonArray = response.getJSONArray("result");
   for(int i=0;i<jsonArray.length;i++)
   {
     JSONObject json     = jsonArray.getJSONObject(i);
     String namaBarang   = json.getString("namaBarang");
     String jumlahBarang = json.getString("jumlahBarang");
     String tglKel       = json.getString("tglKel");

            BarangKeluar bk = new BarangKeluar();
            bk.setNamaBarang(namaBarang);
            bk.setJumlahBarang(jumlahBarang);
            bk.setTglBarang(tglKel);
            bkList.add(bk) 
   }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

you missied JSONArray and "tglBarang" -> "tglKel"

    JSONArray obj = response.getJSONArray("result");
    for (int i = 0; i < obj.length(); i++) {
        String namaBarang = obj.getJSONObject(i).getString("namaBarang");
        String jumlahBarang = obj.getJSONObject(i).getString("jumlahBarang");
        String tglKel = obj.getJSONObject(i).getString("tglKel");
    }
pistolcaffe
  • 849
  • 1
  • 8
  • 15
  • already tried ur code and @IntelliJ Amiya code, both still not working. Do i need to change my method instead into JsonArrayRequest ? – tupir Jun 30 '18 at 11:14