0

I am new to android. I don't know this type of parsing. I want to bind this in Recyclerview

Responce Json

{
    "error": true,
    "callcount": {
        "1": {
            "count": 1,
            "name": "Vipul Dusane"
        },
        "2": {
            "count": 0,
            "name": "Aniket Patil"
        }
    },
    "success": "true",
    "message": "Record Found!"
}

Volley Function

 private void ShowCsllCount() {

    StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.TotalCount ,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        progressDialog.dismissWithAnimation();

                        JSONObject obj = new JSONObject(response);

                        JSONArray dataArray  = obj.getJSONArray("callcount");

                        for (int i = 0; i < dataArray.length(); i++) {
                            JSONObject heroObject = dataArray.getJSONObject(i);

                            totalC_Pojo totalCPojo = new totalC_Pojo
                                    (heroObject.getString("count"),heroObject.getString("name"));

                            dataModelArrayList.add(totalCPojo);
                        }

                        totalCountAd = new TotalCountAd(OtherWR.this, dataModelArrayList);
                        recycler.setAdapter(totalCountAd);
                    } catch (JSONException e) {
                        e.printStackTrace();
                        progressDialog.dismissWithAnimation();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //displaying the error in toast if occurrs
                    progressDialog.dismissWithAnimation();

                    Toast.makeText(OtherWR.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
    // request queue
    RequestQueue requestQueue = Volley.newRequestQueue(OtherWR.this);
    requestQueue.add(stringRequest);
3
  • It is better to use libraries to parse Json than manually writing your code. GSON is a popular Json serializing library supported by google. More information here : github.com/google/gson Commented Jun 10, 2019 at 6:36
  • By looking at your JSON, callcount is JSONObject, not JSONArray but it should be array to work with your code. Commented Jun 10, 2019 at 6:37
  • use nested pojo then parse with gson it ll be works Commented Jun 10, 2019 at 6:40

2 Answers 2

1

try this...

JSONObject callcountObj = new JSONObject(response).getJSONObject("callcount");
    
JSONObject callcount1 = callcountObj.getJSONObject("1");

JSONObject callcount2 = callcountObj.getJSONObject("2");

//callcount one 
String callcount1_count = callcount1.getString("count");
String callcount1_name = callcount1.getString("name");

//callcount two
String callcount2_count = callcount2.getString("count");
String callcount2_name = callcount2.getString("name");
Sign up to request clarification or add additional context in comments.

Comments

0

The json response is incorrect.

Use this response

{
    "error": true,
    "callcount": [
        "1": {
            "count": 1,
            "name": "Vipul Dusane"
        },
        "2": {
            "count": 0,
            "name": "Aniket Patil"
        }
    ],
    "success": "true",
    "message": "Record Found!"
}

instead of

{
    "error": true,
    "callcount": {
        "1": {
            "count": 1,
            "name": "Vipul Dusane"
        },
        "2": {
            "count": 0,
            "name": "Aniket Patil"
        }
    },
    "success": "true",
    "message": "Record Found!"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.