0

I am parsing json using volley but its not working and getting error. Follwing is my code and json reponse. please help me to solve this

 private void getStaffList() {

        showpDialog();
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        final String url = "url";


        try {

            final JSONObject jsonObj = new JSONObject();


            jsonObj.put("username", "test");
            jsonObj.put("password", "123456");


            JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                    url, jsonObj, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("TAG", "Main response=" + response);


                    staffarraylist=new ArrayList<DataModel>();;
                    try {
                        JSONObject jobSuccess=response.getJSONObject("TABLE_DATA");
                        Log.d("TAG", "JSONObj response=" + jobSuccess);
                        JSONArray jarMyData=jobSuccess.getJSONArray("data");
                        Log.d("TAG", "JSONArray response=" + jarMyData);
                        for (int i = 0; i < jarMyData.length(); i++) {
                            JSONArray jar = jarMyData.getJSONArray(i);

                            DataModel movie = new DataModel();
                            movie.setName(jar.getString(0));
                            movie.setOccupation(jar.getString(1));
                            movie.setPlace(jar.getString(2));
                            movie.setId(jar.getString(3));
                            movie.setDate(jar.getString(4));
                            movie.setPrice(jar.getString(5));
                            staffarraylist.add(movie);
                        }
                    }catch (JSONException e)
                    {
                        Log.d("JSONException",e.toString());
                    }


                    rcAdapter = new RecyclerViewAdapterHome(MainActivity.this,  staffarraylist);
                    recyclerView.setAdapter(rcAdapter);

                    hidepDialog();

                }

            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("TAG", "JSONObj Error: " + error.getMessage());
                    hidepDialog();
                    //Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                    // hide the progress dialog
                }
            });

            requestQueue.add(jsonObjReq);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

Logcat

Main response={"TABLE_DATA":"{\"data\":[[\"Tiger Nixon\",\"System Architect\",\"Edinburgh\",\"5421\",\"2011/04/25\",\"$320,800\"],[\"Garrett Winters\",\"Accountant\",....so on

D/JSONException: org.json.JSONException: Value {"data":[["Tiger Nixon","System Architect","Edinburgh","5421","2011/04/25","$320,800"],["Garrett Winters","Accountant","Tokyo","8422","2011/07/25","$

chris
  • 699
  • 4
  • 12
  • 35

1 Answers1

0

First check that your json format is correct or not by clicking here and paste your json for checking. If the response is correct then use Gson. It will parse the response without any error.

Jawad Ahmed
  • 306
  • 1
  • 5
  • 9
  • response is ok, can you tell what is the use of Gson in my case? how to use. – chris Nov 24 '18 at 15:10
  • It will convert your response into a pojo class.So you can use this class instead of converting json manually. – Jawad Ahmed Nov 24 '18 at 15:12
  • can you help me with that please – chris Nov 24 '18 at 15:13
  • Copy paste your json response .It will create a java class for you put it in your project. and then add gson library to your project. – Jawad Ahmed Nov 24 '18 at 15:16
  • it is createing like this public class Test { private String TABLE_DATA; public String getTABLE_DATA () { return TABLE_DATA; } public void setTABLE_DATA (String TABLE_DATA) { this.TABLE_DATA = TABLE_DATA; } @Override public String toString() { return "ClassPojo [TABLE_DATA = "+TABLE_DATA+"]"; } } how will check for data ?? – chris Nov 24 '18 at 15:30
  • Now create a class inside your project with name Test – Jawad Ahmed Nov 24 '18 at 15:35
  • Gson gson = new Gson(); Test test = gson.fromJson(response, Test.class); – Jawad Ahmed Nov 24 '18 at 15:36
  • how to parse data array – chris Nov 24 '18 at 15:56