Getting this error whilt trying to parse JSON data W/System.err: org.json.JSONException: No value for customers

javaNoob :

I am trying to retreive customer object from my rest api. I generated the api using spring data jpa. I have used volley to retrive the information from the api. I can't tell what i did wrong. As i am new to android i don't have much idea. can some one help me to parse the customer object from my Json api.

my api looks like this:

{
  "_embedded": {
    "customers": [
      {
        "firstName": "Alexander",
        "lastName": "arnold",
        "email": "[email protected]",
        "password": "cornertakenquickly",
        "_links": {
          "self": {
            "href": "http://localhost:8080/api/customers/1"
          },
          "customer": {
            "href": "http://localhost:8080/api/customers/1"
          }
        }
      },
      {
        "firstName": "test",
        "lastName": "tester",
        "email": "[email protected]",
        "password": "12345678",
        "_links": {
          "self": {
            "href": "http://localhost:8080/api/customers/2"
          },
          "customer": {
            "href": "http://localhost:8080/api/customers/2"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/customers{?page,size,sort}",
      "templated": true
    },
    "profile": {
      "href": "http://localhost:8080/api/profile/customers"
    }
  },
  "page": {
    "size": 20,
    "totalElements": 2,
    "totalPages": 1,
    "number": 0
  }
}

my code looks like this i am using volley :

  // connects to the api and stores the retrived JSON values in the           respective list
public void connectToApi(){
    // initialize lists
    emailList = new ArrayList<>();
    passwordList = new ArrayList<>();

    final String BASE_URL ="http://192.168.1.67:8080/api/customers";
   // final String BASE_URL ="http://10.0.2.2:8080/api/customers";

    // creating a request ques for HTTP request
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    // Setting HTTP GET request to retrieve the data from the SERVER
    JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET,BASE_URL
            ,null
            , new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

                try {
                    JSONArray jsonArray = response.getJSONArray("customers");
                    for(int i=0; i<jsonArray.length();i++){
                        JSONObject customer = jsonArray.getJSONObject(i);
                        emailList.add(customer.getString("email"));
                        passwordList.add(customer.getString("password"));
                    }

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

        } , new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("REST error",error.toString() );
        }
    }

    );

    requestQueue.add(objectRequest);

}
Abhinav Gupta :

Do this :

@Override
    public void onResponse(JSONObject response) {

            try {
               JSONObject json = new JSONObject(response); 
               JSONObject json_embedded = json.getJSONObject("_embedded");// need to access JSONObject("_embedded")
               JSONArray jsonArray = json_embedded.getJSONArray("customers"); // then get JSONARRAY
                for(int i=0; i<jsonArray.length();i++){
                    JSONObject customer = jsonArray.getJSONObject(i);
                    emailList.add(customer.getString("email"));
                    passwordList.add(customer.getString("password"));
                }

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

NOTE : your json array (customers) is in _embedded that's why it is showing exception.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325011&siteId=1