Search code examples
androidjsongetandroid-volley

Json Data Not Displaying in ListView


I am Trying to parse Json data into a list view The list view is declared, and the Url I use is built with Uri.Builder, I have tested the URL with POSTMAN and it works and displays the data(on PostMan) I am Using Volley to get the data I can not figure out what the problem is The code I use works for populating other list views but as soon as I add more paramaters, It shows blank

My Json data

{"success": 1,"Name": [{"ProjectDescription": "Project Name"},{"TaskCode": "VAL001"},{"TasDescription": "Value text
test"}]}

My Code

  private void LoadTaskSpinner(String url) {
        final ProgressDialog pd=new ProgressDialog(reportActivity.this);
        pd.setMessage("Please Wait..Loading Time Log Data");
        pd.setCanceledOnTouchOutside(false);
        pd.show();
        RequestQueue requestQueue=Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                pd.cancel();
                try {
                    JSONObject jsonObject=new JSONObject(response);
                    if (jsonObject.getInt("success") == 1) {
                        JSONArray jsonArray=jsonObject.getJSONArray("Name");
                        for (int i=0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject1=jsonArray.getJSONObject(i);
                            String task=jsonObject1.getString("ProjectDescription");
                            String code=jsonObject1.getString("TaskCode");
                            String desc=jsonObject1.getString("TasDescription");
                            Project.add(task);
                            Project.add(code);
                            Project.add(desc);
                        }
                    }
                    report.setAdapter(new ArrayAdapter<>(reportActivity.this, android.R.layout.simple_spinner_dropdown_item, Project));
                } catch (JSONException e) {
                    e.printStackTrace();


                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                pd.cancel();

                error.printStackTrace();
            }
        });
        int socketTimeout=30000;
        RetryPolicy policy=new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);
    }

Solution

  • Shiv Kumar, Suggested an Edit for me, This Solved my Issue

      JSONObject jsonObject1=jsonArray.getJSONObject(i);
                            String key=jsonObject1.keys().next();
                            String task=jsonObject1.getString(key);
                            // String code=jsonObject1.getString("TaskCode");
                            // String desc=jsonObject1.getString("TasDescription");
                            Project.add(task);
                            //Project.add(code);
                            //Project.add(desc);